且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

java web 分页解决方案

更新时间:2022-08-17 19:11:25

web开发中经常需要用到分页,我然来的做法是每次需要分页时都把代码copy一份,比如有10个页面有分页,那么我分页的代码就有10个版本.这样导致代码的重用性太低了.

那么如何解决呢?

把分页的页面和逻辑抽取出来,提高代码质量和重用性.

(1)分页的页面抽取出来

Java代码  java web 分页解决方案
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%@ taglib prefix="s" uri="/struts-tags"%>  
  4. <table style="height: 35px">  
  5.     <tr>  
  6.         <td nowrap="nowrap" style="width: 6%">  
  7.         <td nowrap="nowrap" style="width: 63%"><span>共<s:property  
  8.                     value="view.totalRecords" />条记录, 当前第 <s:if  
  9.                     test="view.totalPages==0">0</s:if> <s:else>  
  10.                     <s:property value="view.currentPage" />  
  11.                 </s:else>/ <s:property value="view.totalPages" />页, 每页<s:property  
  12.                     value="view.recordsPerPage" />条记录  
  13.         </span></td>  
  14.         <td nowrap="nowrap">  
  15.             <button type="button" class="btn btn-info" style="padding: 1px 10px"  
  16.                 onclick="toPageFirst(${param.action})">首页</button>  
  17.             <button type="button" class="btn btn-info" style="padding: 1px 10px"  
  18.                 onclick="toPagePre(${param.action})">上一页</button>  
  19.             <button type="button" class="btn btn-info" style="padding: 1px 10px"  
  20.                 onclick="toPageNext(${param.action})">下一页</button>  
  21.             <button type="button" class="btn btn-info" style="padding: 1px 10px"  
  22.                 onclick="toPageLast(${param.action})">尾页</button>  
  23.             <button type="button" class="btn btn-info" style="padding: 1px 10px"  
  24.                 onclick="toPageGo(${param.action})">转</button>   
  25.             <s:if test="view.totalPages==0">  
  26.                 <input  id="view.currentPage"  
  27.                     name="view.currentPage" size="5"  
  28.                       
  29.                       
  30.                     style="margin-bottom: 0px; width: 50px; ime-mode: disabled; text-align: right; padding: 0px 1px 0px 0px; height: 20px; display: inline-block"  
  31.                     value="0" />  
  32.             </s:if> <s:else>  
  33.                 <s:textfield cssClass="form-control" id="view.currentPage"  
  34.                     name="view.currentPage" size="5"  
  35.                     onkeypress="return onlyNumber(event);" onpaste="return false;"  
  36.                       
  37.                     style="margin-bottom: 0px; width: 50px; ime-mode: disabled; text-align: right; padding: 0px 1px 0px 0px; height: 20px; display: inline-block"  
  38.                      />  
  39.             </s:else>   
  40.               
  41.             <input type="hidden" id="view.thisPage" value="<s:property value='view.currentPage' />" />  
  42.             <s:hidden id="view.totalPages" name="view.totalPages"></s:hidden>   
  43.             <s:hidden id="view.ascDesc" name="view.ascDesc"></s:hidden>   
  44.             <s:hidden id="view.sortKey" name="view.sortKey"></s:hidden>  
  45.         </td>  
  46.         <td nowrap="nowrap"><span>页</span></td>  
  47.     </tr>  
  48. </table>  

 (2)在需要分页的页面中引入上述jsp文件

Java代码  java web 分页解决方案
  1. <s:include value="/WEB-INF/jsp/pageBottom.jsp">  
  2.                                 <s:param name="action">usercoupon.query</s:param>  
  3.                             </s:include>  

 注意:必须包含在form表单中

 

 

(3)在后台action中

Java代码  java web 分页解决方案
  1. public String execute() throws Exception {  
  2.         reserveCondition();  
  3.         int start = (Integer.parseInt(view.getCurrentPage()) - 1)  
  4.                 * view.getRecordsPerPage();  
  5.         int count = 0;  
  6.         DetachedCriteria clientVersionDC = DetachedCriteria  
  7.                 .forClass(User.class);  
  8.         condition(clientVersionDC);  
  9.         List list = new ArrayList();  
  10.         count = this.userService.listByDetachedCriteria(list,  
  11.                 clientVersionDC, start, view.getRecordsPerPage());  
  12.         view.setRecordList(list);  
  13.         queryResultList = new ArrayList();  
  14.         queryResultList.addAll(view.getRecordList());  
  15.   
  16.         view.setTotalRecords(count);  
  17.         int totalPages = PageUtil.getTotalPages(view.getTotalRecords(),  
  18.                 view.getRecordsPerPage());  
  19.         view.setTotalPages(String.valueOf(totalPages));  
  20.         return "list";  
  21.     }  

 

在js中声明方法时,要注意,左边是错误的
java web 分页解决方案