一、为什么我们要使用分页展示信息
当我们的需要展示的数据比较多时,这样在我们查找的数据的量也会增加,给数据库的压力还是比较大的,查询时间也会增加;并且在页面的显示的数据也因为显示的数据太多导致不容易使得用户查看,这时我们就会想到让用户方便又能够减轻数据库的压力
二、实现分页的首要条件
这里我们使用的是servlet和jsp来实现,所以我们介绍的时候也是简单分开介绍一下这两部分
三、分析
(1)要实现显示内容分页,首先你要有一些先知条件:当前页数(currentPage)、每页显示的数据的数量(pageCount),以及每页显示的页的连接个数(我们一般就是显示当前页数的-2 到 当前页数 + 2 的范围)。
(2)然后通过数据库可以查找:一共有多少条数据(totalCount)
select count (*) from table_name ;
进而得到一共可以分多少页totalPage
根据这个三个一直条件,我们可以在数据使用limit来进行分页查找List<Entity> pageData
select * from table_name limit currentPage , pageCount;
(3)通过totalCount和totalPage,可以算出来要在页显示哪些页链接:开始(start = currentPage - 2)和结束(end = currentPage + 2)
如果刚开始就给定了start和end,可能会出现这种情况,start < 1 和 end < totalPage的情况,不在显示范围之内,所以我们要在这里设置一下
//分页现实的页数
this.start = 1;
this.end = 5;
if(totalPage<=5){
this.end= this.totalPage;
四、部分代码
jsp:
(1)发出请求
(2)数据显示
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>学生分页展示</title>
</head>
<body>
共有${page.totalCount }个分类,共有${page.totalPage }页,当前为第${page.currentPage }页,每页显示${page.pageCount }条
<table align="center" width="100%">
<thead>
<th>选择</th>
<th>序号</th>
<th>用户名</th>
<th>密码</th>
<th>操作</th>
</thead>
<tbody align="center" border="1">
<c:forEach items="${page.pageData }" var="student">
<tr>
<td><input type="checkbox" name="id"/></td>
<td>${student.id }</td>
<td>${student.username }</td>
<td>${student.password }</td>
<td><a href="${pageContext.request.contextPath }/adminServlet/adminEditStudentServlet?id=${student.id }">修改</a>|<a href="${pageContext.request.contextPath }/adminServlet/adminDeleteStudentServlet?id=${student.id }">删除</a></td>
<td></td>
</tr>
</c:forEach>
</tbody>
</table>
<!-- 分页 -->
<div style="text-align:center;">
<a href="${pageContext.request.contextPath }/adminServlet/findManyStudentServlet?page=1">首页</a>
<!-- 如果当前页为第一页,就没有上一页这个标签 -->
<c:if test="${page.currentPage==1 }">
<c:forEach begin="${page.start }" end="${page.end }" step="1" var="i">
<c:if test="${page.currentPage == i}">
${i}
</c:if>
<c:if test="${page.currentPage != i}">
<a href="${pageContext.request.contextPath}/adminServlet/findManyStudentServlet?page=${i}">${i}</a>
</c:if>
</c:forEach>
<a href="${pageContext.request.contextPath }/adminServlet/findManyStudentServlet?page=${page.currentPage+1}">下一页</a>
</c:if>
<!-- 如果不是首页也不是尾页,就与上一页和下一页 -->
<c:if test="${page.currentPage>1 && page.currentPage<page.totalPage }">
<a href="${pageContext.request.contextPath }/adminServlet/findManyStudentServlet?page=${page.currentPage-1}">上一页</a>
<c:forEach begin="${page.start }" end="${page.end }" step="1" var="i">
<c:if test="${page.currentPage == i}">
${i}
</c:if>
<c:if test="${page.currentPage != i}">
<a href="${pageContext.request.contextPath}/adminServlet/findManyStudentServlet?page=${i}">${i}</a>
</c:if>
</c:forEach>
<a href="${pageContext.request.contextPath }/adminServlet/findManyStudentServlet?page=${page.currentPage+1}">下一页</a>
</c:if>
<!-- 如果是最后一页,则没有下一页 -->
<c:if test="${page.currentPage==page.totalPage }">
<a href="${pageContext.request.contextPath }/adminServlet/findManyStudentServlet?page=${page.currentPage-1}">上一页</a>
<c:forEach begin="${page.start }" end="${page.end }" step="1" var="i">
<c:if test="${page.currentPage == i}">
${i}
</c:if>
<c:if test="${page.currentPage != i}">
<a href="${pageContext.request.contextPath}/adminServlet/findManyStudentServlet?page=${i}">${i}</a>
</c:if>
</c:forEach>
</c:if>
<a href="${pageContext.request.contextPath }/adminServlet/findManyStudentServlet?page=${page.totalPage}">尾页</a>
</div>
</body>
</html>
servlet:
(1)action
public class FindManyStudentServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String pageStr = request.getParameter("page");//当前页数,需要从jsp页面传过来
System.out.println("请求的页面" + pageStr);
int CurrentPage = Integer.parseInt(pageStr);
int pageCount = 6; //每页显示的数量
StudentService service = new StudentService();
PageBean<Student> page = service.findManyStudent(CurrentPage,pageCount);//分页查询
if(page != null){
request.setAttribute("page", page);
request.getRequestDispatcher("/admin/jsp/show.jsp").forward(request, response);
}
}
}
(2)service
//分页查询
public PageBean<Student> findManyStudent(int currentPage, int pageCount) {
int totalCount = dao.findAllCount();//查找一共有多少条数据
PageBean<Student> page = new PageBean<Student>(currentPage,pageCount,totalCount);//创建一个分页查询的工具类对象,下面已经提供了分页查询的工具类,耐心观看
int begin = page.getBegin();
page.setPageData(dao.findManyStudent(begin,pageCount));
return page;
}
(3)dao 声明,这里面我使用了一个工具类和此次分页无关,所以就不在此展示了
public List<Student> findManyStudent(int begin, int pageCount) {
String sql = "select * from students limit ?,?";
List<Student> list = new ArrayList<Student>();
List<Map<String,Object>> listmap= new ArrayList<Map<String,Object>>();
Map<String ,Object> map = new HashMap<String ,Object>();
List<Object> params = new ArrayList<Object>();
params.add(begin);
params.add(pageCount);
dataSourse= new DataUtil();
try {
listmap = dataSourse.ManyResult(sql,params);//使用了工具类
int len = listmap.size();
for(int i = 0 ; i < len ; i++){
map = listmap.get(i);
Student student = new Student();
student.setId((Integer)map.get("id"));
student.setUsername((String)map.get("username"));
student.setPassword((String)map.get("password"));
list.add(student);
}
return list;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
util:
public class PageBean<T> implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private int currentPage ;//当前页数,时页面请求发过来的
private int pageCount;//每页多少数据
private int totalCount;//一共有多少记录
private int totalPage;//计算获得
private int begin;//limit
private List<T> pageData;
private int start;
private int end;
public PageBean(int currentPage,int pageCount,int totalCount){
//可以直接获得
this.currentPage = currentPage;//请求的页数
this.pageCount = pageCount;//每页显示的数量
this.totalCount = totalCount;//一共多少条记录
//进行计算
if(totalCount % pageCount == 0){
this.totalPage = this.totalCount / pageCount;
}else{
this.totalPage = this.totalCount / pageCount + 1;
}
this.begin = (this.currentPage - 1) * pageCount;
//分页现实的页数
this.start = 1;
this.end = 5;
if(totalPage<=5){
this.end= this.totalPage;
}else{
this.start=currentPage-2;
this.end = currentPage+2;
if(start<=0){
this.start=1;
this.end=5;
}
if(this.end > this.totalPage){
this.end=totalPage;
this.start=end-4;
}
}
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getBegin() {
return begin;
}
public void setBegin(int begin) {
this.begin = begin;
}
public List<T> getPageData() {
return pageData;
}
public void setPageData(List<T> pageData) {
this.pageData = pageData;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
}