1、開發前準備javascript
1.參考:https://www.cnblogs.com/liandy0906/p/9985453.htmlhtml
2.目的:爲簡化分頁功能,設計了一個分頁的JSP標籤,只須要在頁面使用分頁標籤,就能夠完成全部頁面的分頁功能。java
2、建立類 web
1.實體類spring
package com.geely.entity; import java.io.Serializable; import java.util.Date; /* * 電池包 */ public class BatteryPackage implements Serializable { //主鍵 private int id; //訂單編號 private String orderNo; //儲能裝置型號 private String systemModelId; //所屬儲能裝置編碼 private String systemId; //文件名 private String fileName; //儲能裝置中的編號 private String serial; //電池包編碼 private String code; //電池包型號 private String modelId; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getOrderNo() { return orderNo; } public void setOrderNo(String orderNo) { this.orderNo = orderNo; } public String getSystemModelId() { return systemModelId; } public void setSystemModelId(String systemModelId) { this.systemModelId = systemModelId; } public String getSystemId() { return systemId; } public void setSystemId(String systemId) { this.systemId = systemId; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getSerial() { return serial; } public void setSerial(String serial) { this.serial = serial; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getModelId() { return modelId; } public void setModelId(String modelId) { this.modelId = modelId; } }
2.工具類 sql
package com.geely.util; /** * 分頁實體 */ public class PageModel { // 默認每頁4條數據 public static int PAGE_DEFAULT_SIZE = 4; /** 分頁總數據條數 */ private int recordCount; /** 當前頁面 */ private int pageIndex ; /** 每頁分多少條數據 */ private int pageSize = PAGE_DEFAULT_SIZE = 4; /** 總頁數 */ private int totalSize; public int getRecordCount() { this.recordCount = this.recordCount <= 0 ? 0:this.recordCount; return recordCount; } public void setRecordCount(int recordCount) { this.recordCount = recordCount; } public int getPageIndex() { this.pageIndex = this.pageIndex <= 0?1:this.pageIndex; /** 判斷當前頁面是否超過了總頁數:若是超過了默認給最後一頁做爲當前頁 */ this.pageIndex = this.pageIndex>=this.getTotalSize()?this.getTotalSize():this.pageIndex; return pageIndex; } public void setPageIndex(int pageIndex) { this.pageIndex = pageIndex; } public int getPageSize() { this.pageSize = this.pageSize <= PAGE_DEFAULT_SIZE?PAGE_DEFAULT_SIZE:this.pageSize; return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalSize() { if(this.getRecordCount() <=0){ totalSize = 0 ; }else{ totalSize = (this.getRecordCount() -1)/this.getPageSize() + 1; } return totalSize; } public int getFirstLimitParam(){ return (this.getPageIndex()-1)*this.getPageSize() ; } }
package com.geely.util; import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.SimpleTagSupport; /** * 分頁標籤 */ public class PagerTag extends SimpleTagSupport { /** 定義請求URL中的佔位符常量 */ private static final String TAG = "{0}"; /** 當前頁碼 */ private int pageIndex; /** 每頁顯示的數量 */ private int pageSize; /** 總記錄條數 */ private int recordCount; /** 請求URL page.action?pageIndex={0}*/ private String submitUrl; /** 樣式 */ private String style = "sabrosus"; /** 定義總頁數 */ private int totalPage = 0; /** 在頁面上引用自定義標籤就會觸發一個標籤處理類 */ @Override public void doTag() throws JspException, IOException { /** 定義它拼接是終的結果 */ StringBuilder res = new StringBuilder(); /** 定義它拼接中間的頁碼 */ StringBuilder str = new StringBuilder(); /** 判斷總記錄條數 */ if (recordCount > 0){ //1499 / 15 = 100 /** 須要顯示分頁標籤,計算出總頁數 須要分多少頁 */ totalPage = (this.recordCount - 1) / this.pageSize + 1; /** 判斷上一頁或下一頁需不須要加a標籤 */ if (this.pageIndex == 1){ // 首頁 str.append("<span class='disabled'>上一頁</span>"); /** 計算中間的頁碼 */ this.calcPage(str); /** 下一頁需不須要a標籤 */ if (this.pageIndex == totalPage){ /** 只有一頁 */ str.append("<span class='disabled'>下一頁</span>"); }else{ String tempUrl = this.submitUrl.replace(TAG, String.valueOf(pageIndex + 1)); str.append("<a href='"+ tempUrl +"'>下一頁</a>"); } }else if (this.pageIndex == totalPage){ // 尾頁 String tempUrl = this.submitUrl.replace(TAG, String.valueOf(pageIndex - 1)); str.append("<a href='"+ tempUrl +"'>上一頁</a>"); /** 計算中間的頁碼 */ this.calcPage(str); str.append("<span class='disabled'>下一頁</span>"); }else{ // 中間 String tempUrl = this.submitUrl.replace(TAG, String.valueOf(pageIndex - 1)); str.append("<a href='"+ tempUrl +"'>上一頁</a>"); /** 計算中間的頁碼 */ this.calcPage(str); tempUrl = this.submitUrl.replace(TAG, String.valueOf(pageIndex + 1)); str.append("<a href='"+ tempUrl +"'>下一頁</a>"); } /** 拼接其它的信息 */ res.append("<table width='100%' align='center' style='font-size:13px;' class='"+ style +"'>"); res.append("<tr><td style='COLOR: #0061de; MARGIN-RIGHT: 3px; PADDING-TOP: 2px; TEXT-DECORATION: none'>" + str.toString()); res.append(" 跳轉到 <input style='text-align: center;BORDER-RIGHT: #aaaadd 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #aaaadd 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 2px; MARGIN: 2px; BORDER-LEFT: #aaaadd 1px solid; COLOR: #000099; PADDING-TOP: 2px; BORDER-BOTTOM: #aaaadd 1px solid; TEXT-DECORATION: none' type='text' size='2' id='pager_jump_page_size'/>"); res.append(" <input type='button' style='text-align: center;BORDER-RIGHT: #dedfde 1px solid; PADDING-RIGHT: 6px; BACKGROUND-POSITION: 50% bottom; BORDER-TOP: #dedfde 1px solid; PADDING-LEFT: 6px; PADDING-BOTTOM: 2px; BORDER-LEFT: #dedfde 1px solid; COLOR: #0061de; MARGIN-RIGHT: 3px; PADDING-TOP: 2px; BORDER-BOTTOM: #dedfde 1px solid; TEXT-DECORATION: none' value='肯定' id='pager_jump_btn'/>"); res.append("</td></tr>"); res.append("<tr align='center'><td style='font-size:13px;'><tr><td style='COLOR: #0061de; MARGIN-RIGHT: 3px; PADDING-TOP: 2px; TEXT-DECORATION: none'>"); /** 開始條數 */ int startNum = (this.pageIndex - 1) * this.pageSize + 1; /** 結束條數 */ int endNum = (this.pageIndex == this.totalPage) ? this.recordCount : this.pageIndex * this.pageSize; res.append("總共<font color='red'>"+ this.recordCount +"</font>條記錄,當前顯示"+ startNum +"-"+ endNum +"條記錄。"); res.append("</td></tr>"); res.append("</table>"); res.append("<script type='text/javascript'>"); res.append(" document.getElementById('pager_jump_btn').onclick = function(){"); res.append(" var page_size = document.getElementById('pager_jump_page_size').value;"); res.append(" if (!/^[1-9]\\d*$/.test(page_size) || page_size < 1 || page_size > "+ this.totalPage +"){"); res.append(" alert('請輸入[1-"+ this.totalPage +"]之間的頁碼!');"); res.append(" }else{"); res.append(" var submit_url = '" + this.submitUrl + "';"); res.append(" window.location = submit_url.replace('"+ TAG +"', page_size);"); res.append(" }"); res.append("}"); res.append("</script>"); }else{ res.append("<table align='center' style='font-size:13px;'><tr><td style='COLOR: #0061de; MARGIN-RIGHT: 3px; PADDING-TOP: 2px; TEXT-DECORATION: none'>總共<font color='red'>0</font>條記錄,當前顯示0-0條記錄。</td></tr></table>"); } this.getJspContext().getOut().print(res.toString()); } /** 計算中間頁碼的方法 */ private void calcPage(StringBuilder str) { /** 判斷總頁數 */ if (this.totalPage <= 11){ /** 一次性顯示所有的頁碼 */ for (int i = 1; i <= this.totalPage; i++){ if (this.pageIndex == i){ /** 當前頁碼 */ str.append("<span class='current'>"+ i +"</span>"); }else{ String tempUrl = this.submitUrl.replace(TAG, String.valueOf(i)); str.append("<a href='"+ tempUrl +"'>"+ i +"</a>"); } } }else{ /** 靠近首頁 */ if (this.pageIndex <= 8){ for (int i = 1; i <= 10; i++){ if (this.pageIndex == i){ /** 當前頁碼 */ str.append("<span class='current'>"+ i +"</span>"); }else{ String tempUrl = this.submitUrl.replace(TAG, String.valueOf(i)); str.append("<a href='"+ tempUrl +"'>"+ i +"</a>"); } } str.append("..."); String tempUrl = this.submitUrl.replace(TAG, String.valueOf(this.totalPage)); str.append("<a href='"+ tempUrl +"'>"+ this.totalPage +"</a>"); } /** 靠近尾頁 */ else if (this.pageIndex + 8 >= this.totalPage){ String tempUrl = this.submitUrl.replace(TAG, String.valueOf(1)); str.append("<a href='"+ tempUrl +"'>1</a>"); str.append("..."); for (int i = this.totalPage - 10; i <= this.totalPage; i++){ if (this.pageIndex == i){ /** 當前頁碼 */ str.append("<span class='current'>"+ i +"</span>"); }else{ tempUrl = this.submitUrl.replace(TAG, String.valueOf(i)); str.append("<a href='"+ tempUrl +"'>"+ i +"</a>"); } } } /** 在中間 */ else{ String tempUrl = this.submitUrl.replace(TAG, String.valueOf(1)); str.append("<a href='"+ tempUrl +"'>1</a>"); str.append("..."); for (int i = this.pageIndex - 4; i <= this.pageIndex + 4; i++){ if (this.pageIndex == i){ /** 當前頁碼 */ str.append("<span class='current'>"+ i +"</span>"); }else{ tempUrl = this.submitUrl.replace(TAG, String.valueOf(i)); str.append("<a href='"+ tempUrl +"'>"+ i +"</a>"); } } str.append("..."); tempUrl = this.submitUrl.replace(TAG, String.valueOf(this.totalPage)); str.append("<a href='"+ tempUrl +"'>"+ this.totalPage +"</a>"); } } } /** setter 方法 */ public void setPageIndex(int pageIndex) { this.pageIndex = pageIndex; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public void setRecordCount(int recordCount) { this.recordCount = recordCount; } public void setSubmitUrl(String submitUrl) { this.submitUrl = submitUrl; } public void setStyle(String style) { this.style = style; } }
3、自定義jsp標籤數據庫
1.在WEB-INF下增長一個tld標籤文件:page.tld express
<?xml version="1.0" encoding="utf-8"?> <taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" version="2.1"> <!-- 描述 自定義標籤版本的一種描述 --> <description>Pager 1.0 core library</description> <!-- 顯示的名稱 導包進行的一個展現 --> <display-name>Pager core</display-name> <!-- 版本號 --> <tlib-version>1.0</tlib-version> <!-- 短名 --> <short-name>fkjava</short-name> <!-- uri :導包 --> <uri>/pager-tags</uri> <!-- 定義一個標籤 --> <tag> <!-- 標籤名 --> <name>pager</name> <!-- 標籤處理類,根據本身的實際文件目錄更改 --> <tag-class>com.geely.util.PagerTag</tag-class> <!-- 設置標籤爲空 --> <body-content>empty</body-content> <!-- 定義標籤的屬性 --> <attribute> <!-- 屬性名 表示分頁的第幾頁 --> <name>pageIndex</name> <!-- 必須的 --> <required>true</required> <!-- run time expression value 爲true支持EL表達式 --> <rtexprvalue>true</rtexprvalue> </attribute> <!-- 定義標籤的屬性 --> <attribute> <!-- 屬性名 表示分頁標籤 ,每頁顯示多少條數據 --> <name>pageSize</name> <!-- 必須的 --> <required>true</required> <!-- run time expression value 爲true支持EL表達式 --> <rtexprvalue>true</rtexprvalue> </attribute> <!-- 定義標籤的屬性 --> <attribute> <!-- 屬性名 記錄分頁的總數 --> <name>recordCount</name> <!-- 必須的 --> <required>true</required> <!-- run time expression value 爲true支持EL表達式 --> <rtexprvalue>true</rtexprvalue> </attribute> <!-- 定義標籤的屬性 --> <attribute> <!-- 屬性名 --> <name>submitUrl</name> <!-- 必須的 --> <required>true</required> <!-- run time expression value 爲true支持EL表達式 --> <rtexprvalue>true</rtexprvalue> </attribute> <!-- 定義標籤的屬性 --> <attribute> <!-- 屬性名 --> <name>style</name> <!-- 必須的 --> <required>false</required> <!-- run time expression value 爲true支持EL表達式 --> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
2.jsp頁面 apache
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+ "/";%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <%@ page language="java" contentType="text/html; charset=UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="f" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!-- 設置一個項目路徑的變量 --> <c:set var="ctx" value="${pageContext.request.contextPath}"></c:set> <!-- 配置分頁標籤 --> <%@ taglib prefix="fkjava" uri="/pager-tags" %> </head> <body> <table> <tr> <th>id</th> <th>systemModelId</th> </tr> <c:forEach items="${list }" var="item"> <tr> <td>${item.id }</td> <td>${item.systemModelId }</td> </tr> </c:forEach> <!-- 分頁標籤 --> <tr valign="top"> <td align="center" class="font3"> <fkjava:pager pageIndex="${requestScope.pageModel.pageIndex}" pageSize="${requestScope.pageModel.pageSize}" recordCount="${requestScope.pageModel.recordCount}" style="digg" submitUrl="${ctx}/package/index?pageIndex={0}" /> </td> <!-- 點擊一次分頁標籤就從新查詢一次數據庫 --> </tr> </table> </body> </html>
3.Mapperapp
@SelectProvider(type=BatteryPackageSqlProvider.class,method="selectByPage") List<BatteryPackage> selectByPage(Map<String, Object> params); @SelectProvider(type=BatteryPackageSqlProvider.class,method="count") Integer count(Map<String, Object> params);
4.Provider.
package com.geely.provider; import java.util.Map; import org.apache.ibatis.jdbc.SQL; import com.geely.entity.BatteryPackage; public class BatteryPackageSqlProvider { // 分頁動態查詢 public String selectByPage(final Map<String, Object> params){ String sql = new SQL(){ { SELECT("*"); FROM("BatteryPackage"); if(params.get("model") != null){ BatteryPackage model = (BatteryPackage)params.get("model"); if(model.getOrderNo() != null && !model.getOrderNo().equals("")){ WHERE(" orderNo LIKE CONCAT('%',#{model.orderNo},'%') "); } } } }.toString(); if(params.get("pageModel") != null){ sql += " limit #{pageModel.firstLimitParam} , #{pageModel.pageSize} "; } return sql; } // 動態查詢總數量 public String count(final Map<String, Object> params){ return new SQL(){ { SELECT("count(*)"); FROM("BatteryPackage"); if(params.get("model") != null){ BatteryPackage model = (BatteryPackage)params.get("model"); if(model.getOrderNo() != null && !model.getOrderNo().equals("")){ WHERE(" orderNo LIKE CONCAT('%',#{model.orderNo},'%') "); } } } }.toString(); } }
4.service
package com.geely.service; import java.util.List; import java.util.Map; import com.geely.entity.BatteryPackage; import com.geely.entity.User; import com.geely.util.PageModel; public interface IBatteryPackageService { List<BatteryPackage> pagedList(BatteryPackage model,PageModel pageModel); }
package com.geely.service; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.geely.dao.IBatteryPackageDao; import com.geely.dao.IUserDao; import com.geely.entity.BatteryPackage; import com.geely.entity.User; import com.geely.util.PageModel; @Service("batteryPackageService") public class BatteryPackageServiceImpl implements IBatteryPackageService { @Resource private IBatteryPackageDao batteryPackageDao; /* * 分頁查詢電池包 * @see com.geely.service.IBatteryPackageService#pagedList(com.geely.entity.BatteryPackage, com.geely.util.PageModel) */ public List<BatteryPackage> pagedList(BatteryPackage model, PageModel pageModel) { Map<String, Object> params=new HashMap<String, Object>(); params.put("model", model); params.put("pageModel", pageModel); int recordCount=batteryPackageDao.count(params); List<BatteryPackage> list=null; if(recordCount>0) { pageModel.setRecordCount(recordCount); list= batteryPackageDao.selectByPage(params); } return list; } }
5.controller
package com.geely.controller; import java.util.List; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import com.geely.entity.BatteryPackage; import com.geely.entity.User; import com.geely.service.IBatteryPackageService; import com.geely.service.IUserService; import com.geely.util.PageModel; @Controller @RequestMapping("/package") public class BatteryPackageController { @Resource private IBatteryPackageService batteryPackageService; @RequestMapping("/index") public String index(HttpServletRequest request,Model model,Integer pageIndex,@ModelAttribute BatteryPackage batteryPackage){ PageModel pageModel=new PageModel(); pageModel.setPageIndex(pageIndex); List<BatteryPackage>list= batteryPackageService.pagedList(batteryPackage, pageModel); model.addAttribute("list", list); model.addAttribute("pageModel", pageModel); return "package/index"; } }
5.entity
package com.geely.entity; import java.io.Serializable; import java.util.Date; /* * 電池包 */ public class BatteryPackage implements Serializable { //主鍵 private int id; //訂單編號 private String orderNo; //儲能裝置型號 private String systemModelId; //所屬儲能裝置編碼 private String systemId; //文件名 private String fileName; //儲能裝置中的編號 private String serial; //電池包編碼 private String code; //電池包型號 private String modelId; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getOrderNo() { return orderNo; } public void setOrderNo(String orderNo) { this.orderNo = orderNo; } public String getSystemModelId() { return systemModelId; } public void setSystemModelId(String systemModelId) { this.systemModelId = systemModelId; } public String getSystemId() { return systemId; } public void setSystemId(String systemId) { this.systemId = systemId; } public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getSerial() { return serial; } public void setSerial(String serial) { this.serial = serial; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getModelId() { return modelId; } public void setModelId(String modelId) { this.modelId = modelId; } }