以下封装了,只要传递给servlet一个字段第几页,就可以实现10页为单位的分页效果了。
MySQL:select * from customer limit m,n;
m:每页开始的记录的索引号。(从0开始)
n:每页显示的条数
每页显示10条
第1页的数据:select * from customer limit 0,10;
第2页的数据:select * from customer limit 10,10;
第3页的数据:select * from customer limit 20,10;
每页开始记录的索引=(当前页码-1)*每页显示的条数
总共多少页=总条数%每页显示的条数==0?总条数/每页显示的条数:总条数/每页显示的条数+1;
domain的改造
增加分页类
1 package cn.itcast.domain; 2 3 import java.util.List; 4 5 //界面上所有与分页有关的都找此类要 6 public class Page { 7 private List records; 8 private int pagesize = 10;//每页显示的记录条数 9 private int pagenum;//用户要看的页码即当前页码 10 private int totalpage;//总页数 11 private int startIndex;//每页开始记录的索引 12 private int totalrecords;//总记录条数 13 14 //显示的页码 15 private int startPage; 16 private int endPage; 17 18 //查询数据时的Servlet的URL 19 private String servletUrl; 20 21 public Page(int pagenum,int totalrecords){ 22 this.pagenum = pagenum; 23 this.totalrecords = totalrecords; 24 25 //计算每页开始记录的索引 26 startIndex = (pagenum-1)*pagesize; 27 //计算总页数 28 totalpage = totalrecords%pagesize==0?totalrecords/pagesize:(totalrecords/pagesize+1); 29 30 //显示的页码 31 if(totalpage<=9){ 32 startPage = 1; 33 endPage = totalpage; 34 }else{ 35 startPage = pagenum-4; 36 endPage = pagenum+4; 37 if(startPage<1){ 38 startPage = 1; 39 endPage = 9; 40 } 41 if(endPage>totalpage){ 42 endPage = totalpage; 43 startPage = totalpage-8; 44 } 45 } 46 } 47 48 49 public List getRecords() { 50 return records; 51 } 52 public void setRecords(List records) { 53 this.records = records; 54 } 55 public int getPagesize() { 56 return pagesize; 57 } 58 public void setPagesize(int pagesize) { 59 this.pagesize = pagesize; 60 } 61 public int getPagenum() { 62 return pagenum; 63 } 64 public void setPagenum(int pagenum) { 65 this.pagenum = pagenum; 66 } 67 public int getTotalpage() { 68 return totalpage; 69 } 70 public void setTotalpage(int totalpage) { 71 this.totalpage = totalpage; 72 } 73 public int getStartIndex() { 74 return startIndex; 75 } 76 public void setStartIndex(int startIndex) { 77 this.startIndex = startIndex; 78 } 79 public int getTotalrecords() { 80 return totalrecords; 81 } 82 public void setTotalrecords(int totalrecords) { 83 this.totalrecords = totalrecords; 84 } 85 86 87 public int getStartPage() { 88 return startPage; 89 } 90 91 92 public void setStartPage(int startPage) { 93 this.startPage = startPage; 94 } 95 96 97 public int getEndPage() { 98 return endPage; 99 } 100 101 102 public void setEndPage(int endPage) { 103 this.endPage = endPage; 104 } 105 106 107 public String getServletUrl() { 108 return servletUrl; 109 } 110 111 112 public void setServletUrl(String servletUrl) { 113 this.servletUrl = servletUrl; 114 } 115 116 }
封装分页的 page.jsp页面
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <!-- 分页的东东 --> 3 第${page.pagenum}页/共${page.totalpage}页 4 <a href="${pageContext.request.contextPath}${page.servletUrl}">首页</a> 5 <a href="${pageContext.request.contextPath}${page.servletUrl}?pagenum=${page.pagenum-1==0?1:page.pagenum-1}">上一页</a> 6 <c:forEach begin="${page.startPage}" end="${page.endPage}" var="num"> 7 <a href="${pageContext.request.contextPath}${page.servletUrl}?pagenum=${num}">${num }</a> 8 </c:forEach> 9 10 <a href="${pageContext.request.contextPath}${page.servletUrl}?pagenum=${page.pagenum+1>page.totalpage?page.totalpage:page.pagenum+1}">下一页</a> 11 <a href="${pageContext.request.contextPath}${page.servletUrl}?pagenum=${page.totalpage}">尾页</a> 12 <select id="s1"> 13 <c:forEach begin="1" end="${page.totalpage}" var="num"> 14 <option value="${num}" ${page.pagenum==num?'selected="selected"':''}>${num}</option> 15 </c:forEach> 16 </select> 17 <a href="javascript:jump()">跳转</a> 18 <script type="text/javascript"> 19 function jump(){ 20 var num = document.getElementById("s1").value; 21 window.location.href="${pageContext.request.contextPath}${page.servletUrl}?pagenum="+num; 22 } 23 </script>
显示页面中增加
<%@include file="/WEB-INF/commons/page.jsp" %>
并在foreach标签中指定 requestScope.page.records字段
参考
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 <%@taglib uri="http://www.WFReduceContent.com" prefix="reduce"%> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 9 <title>Insert title here</title> 10 </head> 11 <body> 12 <form action="${pageContext.request.contextPath }/DelCustomerServlet?method=delMultiple" id="form" method="post"> 13 <table > 14 <tr> 15 <th><a href="${pageContext.request.contextPath}/HandleDispatchServlet?method=toAddCustomer">添加</a></th> 16 <th><a href="javascript:delMultiple()">删除</a></th> 17 </tr> 18 </table> 19 <table align ="center" cellpadding="5" border="4" > 20 <tr> 21 <th>选择</th> 22 <th>姓名</th> 23 <th>性别</th> 24 <th>生日</th> 25 <th>电话</th> 26 <th>email</th> 27 <th>爱好</th> 28 <th>类型</th> 29 <th>描述</th> 30 <th>操作</th> 31 </tr> 32 <c:forEach var="customer" items="${requestScope.page.records }" varStatus="status"> 33 <tr bgcolor="${status.index%2==0?'red':'gray' }"> 34 <td><input type="checkbox" name="ids" value="${customer.id}"/></td> 35 <td>${customer.name}</td> 36 <td>${customer.gender=="1"?"男":"女"}</td> 37 <td>${customer.birthday}</td> 38 <td>${customer.cellphone}</td> 39 <td>${customer.email}</td> 40 <td>${customer.hobby}</td> 41 <td>${customer.type}</td> 42 <td><reduce:reduceContent value="${customer.description}"/> </td> 43 <td><a href="${pageContext.request.contextPath}/HandleDispatchServlet?method=editCustomer&customerId=${customer.id}">编辑</a> 44 <a href="javascript:delOne('${customer.id}')">删除</a></td> 45 </tr> 46 </c:forEach> 47 </table> 48 <script type="text/javascript"> 49 function delOne(customerId) 50 { 51 var sure=window.confirm("确定删除么?"); 52 if(sure) 53 { 54 window.location.href="${pageContext.request.contextPath}/DelCustomerServlet?customerId="+customerId; 55 } 56 } 57 function delMultiple() 58 { 59 var ids=document.getElementsByName("ids"); 60 var selected=false; 61 for(var i=0;i<ids.length;i++) 62 { 63 if(ids[i].checked) 64 { 65 selected=true; 66 break; 67 } 68 } 69 if(selected) 70 { 71 var sure=window.confirm("确定删除所选记录么?"); 72 if(sure) 73 { 74 document.getElementById("form").submit(); 75 } 76 } 77 else 78 { 79 alert("请先选择要删除的记录"); 80 } 81 82 } 83 </script> 84 </form> 85 <%@include file="/WEB-INF/commons/page.jsp" %> 86 </body> 87 88 </html>
DAO层改造:
/**
* 查询记录的总条数
*/
int getTotalRecords();
/**
* 查询分页数据
* @param startIndex 每页开始记录的索引编号
* @param pagesize 每页显示的记录条数
* @return
*/
List<Customer> findPageRecords(int startIndex,int pagesize);
DAO.Impl改造,增加
1 @Override 2 public int getTotalRecords() { 3 int records=0; 4 try { 5 QueryRunner runner=new QueryRunner(JdbcUtil.getDataSource()); 6 String sql="select count(*) from customer"; 7 records=(int) (long)runner.query(sql,new ScalarHandler()); 8 } catch (SQLException e) { 9 e.printStackTrace(); 10 } 11 return records; 12 13 } 14 15 @Override 16 public List<Customer> findPageRecords(int startIndex, int pagesize) { 17 List<Customer> list=null; 18 try { 19 QueryRunner runner=new QueryRunner(JdbcUtil.getDataSource()); 20 String sql="select * from customer limit ? ,?"; 21 Object params[]={startIndex,pagesize}; 22 list=runner.query(sql,new BeanListHandler<Customer>(Customer.class),params); 23 } catch (SQLException e) { 24 e.printStackTrace(); 25 } 26 return list; 27 }
Service改造:
/**
* 根据用户要看的页码返回封装了分页有关数据的Page对象
*/
Page findPageReocrds(String pagenum);
Service.Impl改造 增加
1 @Override 2 public Page findPageRecords(String pagenum) { 3 int num = 1;//默认值 4 if(pagenum!=null&&!"".equals(pagenum.trim()))//如果用户传进来的是null或者是空字符串,则说明第一次访问,默认显示的页码是第1页 5 num = Integer.parseInt(pagenum); 6 int totalrecords = dao.getTotalRecords(); 7 Page page = new Page(num, totalrecords);//构造Page对象,因为需要当前页码和总记录条数,所以先通过DAO查出总记录条数 8 //Page对象一旦构建出来,那么每页开始记录的索引、总页数等都计算出来了 9 //但是Page对象中的分页记录还木有,所以又通过Dao查询的分页记录 10 //DAO查询分页记录需要每页开始记录的索引和每页显示的条数,这两个参数Page对象中已经计算完毕了 11 List records = dao.findPageRecords(page.getStartIndex(), page.getPagesize());//查询出记录,并设置到Page对象中。这样,任何与分页有关的信息都在Page对象中了 12 page.setRecords(records); 13 return page; 14 //return dao.findPageRecords(startIndex, pagesize); 15 }
Servlet改造: 在page.jsp中传递了 pagenum参数,进行查找数据库返回一定的数据。
1 String pagenum = request.getParameter("pagenum");//用户要看的页码 2 Page page = s.findPageReocrds(pagenum); 3 page.setServletUrl("/servlet/ShowAllCustomersServlet"); //显示数据的Servlet,和servlet相关的地址映射 4 request.setAttribute("page", page); //存放的对象 5 request.getRequestDispatcher("/listCustomers.jsp").forward(request, response);