分页测试页面:emp.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %> 
  6. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
  7.  
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
  9. <html> 
  10.   <head> 
  11.     <base href="<%=basePath%>"> 
  12.       
  13.     <title>EMP查询页面</title> 
  14.     <script type="text/javascript"> 
  15.         function find(pageCurrent){  
  16.             //默认显示的分页大小  
  17.             var pageSize = 10;  
  18.             //新输入的分页大小  
  19.             var newPageSize = document.getElementById("newPageSize").value;              
  20.             if(newPageSize!="")  
  21.             {  
  22.                 pageSize = newPageSize;  
  23.             }  
  24.             document.getElementById("form").action = "empPage.do?method=findEmp&pageCurrent=" + pageCurrent+"&pageSize=" + pageSize;  
  25.             document.getElementById("form").submit();  
  26.         }  
  27.     </script> 
  28.   </head> 
  29.     
  30.   <body> 
  31.     <form id="form" action="" method="post">          
  32.         <table align="center" border="1" width="500px" style="border-collapse: collapse;border: 1 solid #CCC"> 
  33.             <tr> 
  34.                 <td align="center" colspan="4">查询条件</td> 
  35.             </tr> 
  36.             <tr> 
  37.                 <td>员工编号:</td> 
  38.                 <td> 
  39.                     <input type="text" name="empNo" value="${empForm.empNo}"> 
  40.                 </td> 
  41.                 <td>员工姓名:</td> 
  42.                 <td> 
  43.                     <input type="text" name="ename" value="${empForm.ename}"> 
  44.                 </td> 
  45.             </tr> 
  46.             <tr> 
  47.                 <td align="center" colspan="4"> 
  48.                     <input type="button" value="查询" onclick="find(1)"> 
  49.                 </td> 
  50.             </tr> 
  51.         </table> 
  52.     </form> 
  53.     <table align="center" border="1" width="500px" style="border-collapse: collapse;border: 1 solid #CCC"> 
  54.         <tr> 
  55.             <td width="100px">请选择</td> 
  56.             <td width="200px">员工编号</td>   
  57.             <td width="200px">员工姓名</td> 
  58.         </tr> 
  59.         <c:forEach items="${list}" var="emp">     
  60.         <tr> 
  61.             <td><input type="radio" name="emp1" value="${emp.empNo}"></td> 
  62.             <td>${emp.empNo}</td> 
  63.             <td>${emp.ename}</td> 
  64.         </tr> 
  65.         </c:forEach> 
  66.         <c:if test="${empForm.pageCount == 0 || empForm.pageCount == null}"> 
  67.             <tr> 
  68.                 <td colspan="3" align="center">没有找到记录!</td> 
  69.             </tr> 
  70.         </c:if> 
  71.         <tr> 
  72.             <td colspan="3" align="center">                  
  73.                 <a href="javascript:find(1)">首页</a> 
  74.                 <a href="javascript:find(${(empForm.pageCurrent-1)<=0?1:(empForm.pageCurrent-1)})">上一页</a> 
  75.                 <a href="javascript:find(${(empForm.pageCurrent+1)>(empForm.pageCount)?(empForm.pageCount):(empForm.pageCurrent+1)})">下一页</a> 
  76.                 <a href="javascript:find(${empForm.pageCount})">最后一页</a> 
  77.                    每页<input type="text" id="newPageSize" size="1" value="${empForm.pageSize}">条  
  78.                 <c:if test="${empForm.pageCount!=0&&empForm.pageCount!=null}"> 
  79.                    第${empForm.pageCurrent}页    共${empForm.pageCount}页  
  80.                 </c:if> 
  81.             </td> 
  82.         </tr> 
  83.     </table> 
  84.   </body> 
  85. </html> 

公共的分页form,继承ActionForm:pageForm.java

  1. public class PageForm extends ActionForm  
  2. {  
  3.     private static final long serialVersionUID = -5529397891510258006L;  
  4.  
  5.     private int pageCurrent;  //当前页
  6.       
  7.     private int pageSize;   //分页大小
  8.       
  9.     private int pageCount;   //总页数
  10.       
  11.     private int rowCount;   //总记录数
  12.       
  13.     public void setPageCurrent(int pageCurrent)  
  14.     {  
  15.         this.pageCurrent = pageCurrent;  
  16.     }  
  17.     public int getPageCurrent()  
  18.     {  
  19.         return pageCurrent;  
  20.     }          
  21.     public void setPageSize(int pageSize)  
  22.     {  
  23.         this.pageSize = pageSize;  
  24.     }  
  25.     public int getPageSize()  
  26.     {  
  27.         return pageSize;  
  28.     }  
  29.     public void setPageCount(int pageCount)  
  30.     {  
  31.         this.pageCount = pageCount;  
  32.     }  
  33.     public int getPageCount()  
  34.     {  
  35.         return pageCount;  
  36.     }  
  37.     public void setRowCount(int rowCount)  
  38.     {  
  39.         this.rowCount = rowCount;  
  40.         this.pageCount = (rowCount % pageSize == 0) ? (rowCount / pageSize): (rowCount / pageSize + 1);  
  41.     }  
  42.     public int getRowCount()  
  43.     {  
  44.         return rowCount;  
  45.     }