标签:
由于最近项目需要,于是就简单写了个分页查询。总体而言MySql 分页机制较为简单。数据库方面只需要使用limit即可实现分页。前后台交互就直接用session传了值。
下面就写写具体的实现过程:
首先,创建一个Page VO类
其中,总记录数可由数据库查得、每页默认记录数由你自己设定;
总页数由总记录数及每页默认记录数获取,当然得有一个条件判断;
当前页数由当前记录数及每页默认记录数获取,当然这也得有一个条件判断;
当前记录条数可由页面传值同时配合每页默认记录数获取。
1 package com.ctrl.vo; 2 3 public class Page { 4 private int totalPage;// 总页数 5 private int currentPage;// 当前页 6 private int totalRecord;// 总记录数 7 private int currentRecord=0;// 当前记录条数 8 private int pageSize = 10;// 每页默认记录数 9 10 public int getTotalPage() { 11 return totalPage; 12 } 13 14 public void setTotalPage(int totalRecord, int pageSize) { 15 if (totalRecord % pageSize == 0) 16 this.totalPage = totalRecord / pageSize; 17 else 18 this.totalPage = totalRecord / pageSize + 1; 19 } 20 21 public int getCurrentPage() { 22 return currentPage; 23 } 24 25 public void setCurrentPage(int currentRecord, int pageSize) { 26 if (currentRecord % pageSize == 0) 27 this.currentPage = currentRecord / pageSize; 28 else 29 this.currentPage = currentRecord / pageSize + 1; 30 } 31 32 public int getTotalRecord() { 33 return totalRecord; 34 } 35 36 public void setTotalRecord(int totalRecord) { 37 this.totalRecord = totalRecord; 38 } 39 40 public int getCurrentRecord() { 41 return currentRecord; 42 } 43 44 public void setCurrentRecord(int currentRecord) { 45 this.currentRecord = currentRecord; 46 } 47 48 @Override 49 public String toString() { 50 return "Page [totalPage=" + totalPage + ", currentPage=" + currentPage 51 + ", totalRecord=" + totalRecord + ", currentRecord=" 52 + currentRecord + ", pageSize=" + pageSize + "]"; 53 } 54 55 public int getPageSize() { 56 return pageSize; 57 } 58 59 public void setPageSize(int pageSize) { 60 this.pageSize = pageSize; 61 } 62 63 }
其次,showUsers.jsp页面(有省略,且部分名修改)
<c:choose> <c:when test="${users != null }"> <br> <form action="#" method="post"> <table class="table table-striped table-bordered"> <thead> <tr> <td>ID</td> <td>NAME</td> <td>CODE</td> <td>PWD</td> <td>STATE</td> <td>FLAG</td> <td>Update</td> </tr> </thead> <c:forEach items="${users }" var="user"> <tr> <td>${user.id }</td> <td>${user.name }</td> <td>${user.code }</td> <td>${user.pwd }</td> <td>${user.state }</td> <td>${user.flag }</td> <td><a href="update.jsp?user=${user }">update</a></td> </tr> </c:forEach> </table> </form> <c:if test="${flag==‘T‘}"> <a href="<%=request.getContextPath()%>/showLimitUsers?currentRecord=${page.currentRecord}&type=prev">前一页</a> /<a href="<%=request.getContextPath()%>/showLimitUsers?currentRecord=${page.currentRecord}&type=next"> 后一页</a> </c:if>
.....
对于,servlet后台(SERVICE、DAO层代码省略...)
Page page = new Page(); UserService userService = new UserServiceImpl(); //去掉管理员 long totalRecord = userService.getUsersCount() - 1; page.setTotalRecord((int) totalRecord); page.setTotalPage(page.getTotalRecord(), page.getPageSize()); String type = request.getParameter("type"); if (request.getParameter("currentRecord") != null && !request.getParameter("currentRecord").equals("")) { int currentRecord = Integer.parseInt(request .getParameter("currentRecord")); page.setCurrentRecord(currentRecord); page.setCurrentPage(page.getCurrentRecord(), page.getPageSize()); if ("next".equals(type) && page.getCurrentPage() < (page.getTotalPage() - 1)) { currentRecord = currentRecord + 10; page.setCurrentRecord(currentRecord); } else if ("prev".equals(type) && page.getCurrentPage() > 0) { currentRecord = currentRecord - 10; page.setCurrentRecord(currentRecord); } } else { page.setCurrentRecord(0); } List<User> users = userService.getLimitUsers(page.getCurrentRecord(), page.getPageSize()); page.setCurrentPage(page.getCurrentRecord(), page.getPageSize()); HttpSession session = request.getSession(); //<a>标签是否显示 session.setAttribute("flag", "T"); session.setAttribute("users", users); session.setAttribute("page", page); response.sendRedirect(request.getContextPath() + "/showUsers.jsp");
当用户点击了next,当前记录数就加pageSize,prev则减去pageSize。同时当前页也需要与总页数之间进行比较,以防超越了页数范围。
由于未将所有功能均设置分页,故设定flag标志,当页面其他功能不需要分页的话,可以将分页<a>消除。
效果图:
PS:文中Page VO为借鉴而来。
心得:
分页不难,但还是会出现bug,需要好好调试。可以通过打印Page&将所传的值在input标签显示方便调试。
标签:
原文地址:http://www.cnblogs.com/zhaoww/p/5135745.html