当页面中要显示的内容过多需要分多页显示、或是数据量过大内存吃不消时,需要分页处理。
原理:每次从数据库中取出一定量的数据,通过jsp页面显示
实现:
①写一个类封装分页的页面
②从数据库中取出一个页面的数据,将信息封装到分页页面对象中
③根据情况,将分页的页面对象设置到request对象、session对象或servletContext对象属性中,供jsp页面调用
④在jsp页面中显示分页数据、分页页码、上一页下一页、跳转页面等
下面看具体代码:
分页页面Page类:
1 package cn.wzbrilliant.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 public Page(int pagenum,int totalrecords){ 19 this.pagenum = pagenum; 20 this.totalrecords = totalrecords; 21 22 //计算每页开始记录的索引 23 startIndex = (pagenum-1)*pagesize; 24 //计算总页数 25 totalpage = totalrecords%pagesize==0?totalrecords/pagesize:(totalrecords/pagesize+1); 26 27 //显示的页码 28 if(totalpage<=9){ 29 startPage = 1; 30 endPage = totalpage; 31 }else{ 32 startPage = pagenum-4; 33 endPage = pagenum+4; 34 if(startPage<1){ 35 startPage = 1; 36 endPage = 9; 37 } 38 if(endPage>totalpage){ 39 endPage = totalpage; 40 startPage = totalpage-8; 41 } 42 } 43 } 44 45 public List getRecords() { 46 return records; 47 } 48 49 public void setRecords(List records) { 50 this.records = records; 51 } 52 53 public int getPagesize() { 54 return pagesize; 55 } 56 57 public void setPagesize(int pagesize) { 58 this.pagesize = pagesize; 59 } 60 public int getPagenum() { 61 return pagenum; 62 } 63 64 public void setPagenum(int pagenum) { 65 this.pagenum = pagenum; 66 } 67 68 public int getTotalpage() { 69 return totalpage; 70 } 71 72 public void setTotalpage(int totalpage) { 73 this.totalpage = totalpage; 74 } 75 76 public int getStartIndex() { 77 return startIndex; 78 } 79 public void setStartIndex(int startIndex) { 80 this.startIndex = startIndex; 81 } 82 83 public int getTotalrecords() { 84 return totalrecords; 85 } 86 87 public void setTotalrecords(int totalrecords) { 88 this.totalrecords = totalrecords; 89 } 90 91 public int getStartPage() { 92 return startPage; 93 } 94 95 public void setStartPage(int startPage) { 96 this.startPage = startPage; 97 } 98 99 public int getEndPage() { 100 return endPage; 101 } 102 103 public void setEndPage(int endPage) { 104 this.endPage = endPage; 105 } 106 107 }
从数据库中取出一定条数的记录,此处以mysql为例,sql语句为select * from 表名 limit ?,? 两个问号分别为起始位置和结束位置
在jsp页面中显示分页页码、上一页下一页、跳转页面等的实现代码:
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <!-- 分页的部分 --> 3 第${page.pagenum}页/共${page.totalpage}页 4 <a href="${pageContext.request.contextPath}/servlet/ShowAllMessageServlet">首页</a> 5 <a href="${pageContext.request.contextPath}/servlet/ShowAllMessageServlet?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}/servlet/ShowAllMessageServlet?pagenum=${num}">${num }</a> 8 </c:forEach> 9 10 <a href="${pageContext.request.contextPath}/servlet/ShowAllMessageServlet?pagenum=${page.pagenum+1>page.totalpage?page.totalpage:page.pagenum+1}">下一页</a> 11 <a href="${pageContext.request.contextPath}/servlet/ShowAllMessageServlet?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 19 <script type="text/javascript"> 20 function jump(){ 21 var num = document.getElementById("s1").value; 22 window.location.href="${pageContext.request.contextPath}/servlet/ShowAllCustomersServlet?pagenum="+num; 23 } 24 </script>