当页面中要显示的内容过多需要分多页显示、或是数据量过大内存吃不消时,需要分页处理。
原理:每次从数据库中取出一定量的数据,通过jsp页面显示
实现:
①写一个类封装分页的页面
②从数据库中取出一个页面的数据,将信息封装到分页页面对象中
③根据情况,将分页的页面对象设置到request对象、session对象或servletContext对象属性中,供jsp页面调用
④在jsp页面中显示分页数据、分页页码、上一页下一页、跳转页面等
下面看具体代码:
分页页面Page类:
package cn.wzbrilliant.domain; import java.util.List; //界面上所有与分页有关的都找此类要 public class Page { private List records; private int pagesize = 10;//每页显示的记录条数 private int pagenum;//用户要看的页码即当前页码 private int totalpage;//总页数 private int startIndex;//每页开始记录的索引 private int totalrecords;//总记录条数 //显示的页码 private int startPage; private int endPage; public Page(int pagenum,int totalrecords){ this.pagenum = pagenum; this.totalrecords = totalrecords; //计算每页开始记录的索引 startIndex = (pagenum-1)*pagesize; //计算总页数 totalpage = totalrecords%pagesize==0?totalrecords/pagesize:(totalrecords/pagesize+1); //显示的页码 if(totalpage<=9){ startPage = 1; endPage = totalpage; }else{ startPage = pagenum-4; endPage = pagenum+4; if(startPage<1){ startPage = 1; endPage = 9; } if(endPage>totalpage){ endPage = totalpage; startPage = totalpage-8; } } } public List getRecords() { return records; } public void setRecords(List records) { this.records = records; } public int getPagesize() { return pagesize; } public void setPagesize(int pagesize) { this.pagesize = pagesize; } public int getPagenum() { return pagenum; } public void setPagenum(int pagenum) { this.pagenum = pagenum; } public int getTotalpage() { return totalpage; } public void setTotalpage(int totalpage) { this.totalpage = totalpage; } public int getStartIndex() { return startIndex; } public void setStartIndex(int startIndex) { this.startIndex = startIndex; } public int getTotalrecords() { return totalrecords; } public void setTotalrecords(int totalrecords) { this.totalrecords = totalrecords; } public int getStartPage() { return startPage; } public void setStartPage(int startPage) { this.startPage = startPage; } public int getEndPage() { return endPage; } public void setEndPage(int endPage) { this.endPage = endPage; } }
从数据库中取出一定条数的记录,此处以mysql为例,sql语句为select * from 表名 limit ?,? 两个问号分别为起始位置和结束位置
在jsp页面中显示分页页码、上一页下一页、跳转页面等的实现代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!-- 分页的部分 --> 第${page.pagenum}页/共${page.totalpage}页 <a href="${pageContext.request.contextPath}/servlet/ShowAllMessageServlet">首页</a> <a href="${pageContext.request.contextPath}/servlet/ShowAllMessageServlet?pagenum=${page.pagenum-1==0?1:page.pagenum-1}">上一页</a> <c:forEach begin="${page.startPage}" end="${page.endPage}" var="num"> <a href="${pageContext.request.contextPath}/servlet/ShowAllMessageServlet?pagenum=${num}">${num }</a> </c:forEach> <a href="${pageContext.request.contextPath}/servlet/ShowAllMessageServlet?pagenum=${page.pagenum+1>page.totalpage?page.totalpage:page.pagenum+1}">下一页</a> <a href="${pageContext.request.contextPath}/servlet/ShowAllMessageServlet?pagenum=${page.totalpage}">尾页</a> <select id="s1"> <c:forEach begin="1" end="${page.totalpage}" var="num"> <option value="${num}" ${page.pagenum==num?'selected="selected"':''}>${num}</option> </c:forEach> </select> <a href="javascript:jump()">跳转</a> <script type="text/javascript"> function jump(){ var num = document.getElementById("s1").value; window.location.href="${pageContext.request.contextPath}/servlet/ShowAllCustomersServlet?pagenum="+num; } </script>
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u013805360/article/details/48086445