查询的分页,在web中经常用到。一般,分页要维护的信息很多,我们把这些相关的信息,分装到一个类中,PageBean。具体如下:
1 package cn.itcast.utils; 2 3 import java.util.List; 4 5 import cn.itcast.entity.Employee; 6 7 /** 8 * 封装分页的参数 9 * 10 * @author Jie.Yuan 11 * 12 */ 13 public class PageBean<T> { 14 private int currentPage = 1; // 当前页, 默认显示第一页 15 private int pageCount = 4; // 每页显示的行数(查询返回的行数), 默认每页显示4行 16 private int totalCount; // 总记录数 17 private int totalPage; // 总页数 = 总记录数 / 每页显示的行数 (+ 1) 18 private List<T> pageData; // 分页查询到的数据 19 20 // 返回总页数 21 public int getTotalPage() { 22 if (totalCount % pageCount == 0) { 23 totalPage = totalCount / pageCount; 24 } else { 25 totalPage = totalCount / pageCount + 1; 26 } 27 return totalPage; 28 } 29 public void setTotalPage(int totalPage) { 30 this.totalPage = totalPage; 31 } 32 33 public int getCurrentPage() { 34 return currentPage; 35 } 36 public void setCurrentPage(int currentPage) { 37 this.currentPage = currentPage; 38 } 39 public int getPageCount() { 40 return pageCount; 41 } 42 public void setPageCount(int pageCount) { 43 this.pageCount = pageCount; 44 } 45 public int getTotalCount() { 46 return totalCount; 47 } 48 public void setTotalCount(int totalCount) { 49 this.totalCount = totalCount; 50 } 51 52 public List<T> getPageData() { 53 return pageData; 54 } 55 public void setPageData(List<T> pageData) { 56 this.pageData = pageData; 57 } 58 59 60 61 }
我们在servlet层实现数据的查询。一般的做法是这样的,我们在servlet中维护一个pageBean的实例,在servlet中调用查询,并把pageBean作为参数传入。当查询结束后,pageBean的参数已经是查询后的记过,servlet再把查询后的结果,也就是封装在pageBean中的信息保存到域中发送到jsp页面。下面是servlet中的内容:
1 /** 2 * 4. 控制层开发 3 * @author Jie.Yuan 4 * 5 */ 6 public class IndexServlet extends HttpServlet { 7 // 创建Service实例 8 private IEmployeeService employeeService = new EmployeeService(); 9 // 跳转资源 10 private String uri; 11 12 public void doGet(HttpServletRequest request, HttpServletResponse response) 13 throws ServletException, IOException { 14 15 try { 16 //1. 获取“当前页”参数; (第一次访问当前页为null) 17 String currPage = request.getParameter("currentPage"); 18 // 判断 19 if (currPage == null || "".equals(currPage.trim())){ 20 currPage = "1"; // 第一次访问,设置当前页为1; 21 } 22 // 转换 23 int currentPage = Integer.parseInt(currPage); 24 25 //2. 创建PageBean对象,设置当前页参数; 传入service方法参数 26 PageBean<Employee> pageBean = new PageBean<Employee>(); 27 pageBean.setCurrentPage(currentPage); 28 29 //3. 调用service 30 employeeService.getAll(pageBean); // 【pageBean已经被dao填充了数据】 31 32 //4. 保存pageBean对象,到request域中 33 request.setAttribute("pageBean", pageBean); 34 35 //5. 跳转 36 uri = "/WEB-INF/list.jsp"; 37 } catch (Exception e) { 38 e.printStackTrace(); // 测试使用 39 // 出现错误,跳转到错误页面;给用户友好提示 40 uri = "/error/error.jsp"; 41 } 42 request.getRequestDispatcher(uri).forward(request, response); 43 44 } 45 46 public void doPost(HttpServletRequest request, HttpServletResponse response) 47 throws ServletException, IOException { 48 this.doGet(request, response); 49 } 50 51 }
下面是dao层查询的内容:
1 /** 2 * 2. 数据访问层实现 3 * @author Jie.Yuan 4 * 5 */ 6 public class EmployeeDao implements IEmployeeDao { 7 8 @Override 9 public void getAll(PageBean<Employee> pb) { 10 11 //2. 查询总记录数; 设置到pb对象中 12 int totalCount = this.getTotalCount(); 13 pb.setTotalCount(totalCount); 14 15 /* 16 * 问题: jsp页面,如果当前页为首页,再点击上一页报错! 17 * 如果当前页为末页,再点下一页显示有问题! 18 * 解决: 19 * 1. 如果当前页 <= 0; 当前页设置当前页为1; 20 * 2. 如果当前页 > 最大页数; 当前页设置为最大页数 21 */ 22 // 判断 23 if (pb.getCurrentPage() <=0) { 24 pb.setCurrentPage(1); // 把当前页设置为1 25 } else if (pb.getCurrentPage() > pb.getTotalPage()){ 26 pb.setCurrentPage(pb.getTotalPage()); // 把当前页设置为最大页数 27 } 28 29 //1. 获取当前页: 计算查询的起始行、返回的行数 30 int currentPage = pb.getCurrentPage(); 31 int index = (currentPage -1 ) * pb.getPageCount(); // 查询的起始行 32 int count = pb.getPageCount(); // 查询返回的行数 33 34 35 //3. 分页查询数据; 把查询到的数据设置到pb对象中 36 String sql = "select * from employee limit ?,?"; 37 38 try { 39 // 得到Queryrunner对象 40 QueryRunner qr = JdbcUtils.getQueryRuner(); 41 // 根据当前页,查询当前页数据(一页数据) 42 List<Employee> pageData = qr.query(sql, new BeanListHandler<Employee>(Employee.class), index, count); 43 // 设置到pb对象中 44 pb.setPageData(pageData); 45 46 } catch (Exception e) { 47 throw new RuntimeException(e); 48 } 49 50 } 51 52 @Override 53 public int getTotalCount() { 54 String sql = "select count(*) from employee"; 55 try { 56 // 创建QueryRunner对象 57 QueryRunner qr = JdbcUtils.getQueryRuner(); 58 // 执行查询, 返回结果的第一行的第一列 59 Long count = qr.query(sql, new ScalarHandler<Long>()); 60 return count.intValue(); 61 } catch (Exception e) { 62 throw new RuntimeException(e); 63 } 64 } 65 66 }
最后jsp页面吧查询的结果,从域中拿出来展示:
1 <body> 2 <table border="1" width="80%" align="center" cellpadding="5" cellspacing="0"> 3 <tr> 4 <td>序号</td> 5 <td>员工编号</td> 6 <td>员工姓名</td> 7 </tr> 8 <!-- 迭代数据 --> 9 <c:choose> 10 <c:when test="${not empty requestScope.pageBean.pageData}"> 11 <c:forEach var="emp" items="${requestScope.pageBean.pageData}" varStatus="vs"> 12 <tr> 13 <td>${vs.count }</td> 14 <td>${emp.empId }</td> 15 <td>${emp.empName }</td> 16 </tr> 17 </c:forEach> 18 </c:when> 19 <c:otherwise> 20 <tr> 21 <td colspan="3">对不起,没有你要找的数据</td> 22 </tr> 23 </c:otherwise> 24 </c:choose> 25 26 <tr> 27 <td colspan="3" align="center"> 28 当前${requestScope.pageBean.currentPage }/${requestScope.pageBean.totalPage }页 29 30 <a href="${pageContext.request.contextPath }/index?currentPage=1">首页</a> 31 <a href="${pageContext.request.contextPath }/index?currentPage=${requestScope.pageBean.currentPage-1}">上一页 </a> 32 <a href="${pageContext.request.contextPath }/index?currentPage=${requestScope.pageBean.currentPage+1}">下一页 </a> 33 <a href="${pageContext.request.contextPath }/index?currentPage=${requestScope.pageBean.totalPage}">末页</a> 34 </td> 35 </tr> 36 37 </table> 38 </body>