JavaWeb分页的实现代码实例

时间:2021-05-19

这篇文章主要介绍了JavaWeb分页的实现代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

分页的分类

分页的实现分为真分页和假分页两种。

1.真分页(物理分页):

实现原理: SELECT * FROM xxx [WHERE...] LIMIT ?, 10;

第一个参数是开始数据的索引位置

10是要查询多少条数据,即每页显示的条数

优点: 不会造成内存溢出

缺点: 翻页的速度比较慢

2.假分页(逻辑分页):

实现原理: 一次性将所有的数据查询出来放在内存之中,每次需要查询的时候就直接从内存之中去取出相应索引区间的数据

优点: 分页的速度比较快

缺点: 可能造成内存溢出

分页的一些术语:

  • -- 数据总条数: totalCount : select count(1) from t_user;
  • -- 每页显示条数:pageSize
  • -- 总页数:totalPage
  • -- 当前页:currPage
  • -- 起始索引: startIndex

-- 通过当前页码查询第几页的数据

select * from t_user limit 0, 5; -- 页码 1
select * from t_user limit 5, 5; -- 页码 2
select * from t_user limit 10, 5; -- 页码 3

-- 公式:startIndex = (currPage - 1) * pageSize

-- 计算一共有多少页

-- 方法一:result = totalCount%pageSize,如果余数result为0,

-- totalPage = totalCount / pageSize

-- 如果余数result不为0,

-- totalPage = totalCount / pageSize + 1;

-- 方法二:totalPage = (totalCount + pageSize - 1) / pageSize

Pageing工具类

public class PaginationBean<T> { private List<T> dataList; private int currPage; private int totalPage; public List<T> getDataList() { return dataList; } public void setDataList(List<T> dataList) { this.dataList = dataList; } public int getCurrPage() { return currPage; } public void setCurrPage(int currPage) { this.currPage = currPage; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; }}

Servlet

@WebServlet("/showUserList")public class ShowUserListServlet extends HttpServlet implements Servlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String operation = request.getParameter("operation"); String currPageStr = request.getParameter("currPage"); int currPage = 0; IUserService userSevice = new UserServiceImpl(); int totalPage = userSevice.getTotalPage(); if ("首页".equals(operation) || operation == null || currPageStr == null || currPageStr.length() == 0) { currPage = 1; } else if ("上一页".equals(operation)) { currPage = Integer.parseInt(currPageStr) - 1; if (currPage <= 0) { currPage = 1; } } else if ("下一页".equals(operation)) { currPage = Integer.parseInt(currPageStr) + 1; if (currPage >= totalPage) { currPage = totalPage; } } else { currPage = totalPage; } List<TestUserBean> userList = userSevice.getUserListByCurrPage(currPage); PaginationBean<TestUserBean> pageBean = new PaginationBean<TestUserBean>(); pageBean.setDataList(userList); pageBean.setCurrPage(currPage); pageBean.setTotalPage(totalPage); request.setAttribute("page", pageBean); request.getRequestDispatcher("/userList.jsp").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}

jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%-- 引入JSTL --%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><style> table { border-collapse: collapse; }</style></head><body> <table border="1"> <tr> <th>ID</th> <th>姓名</th> <th>密码</th> <th>身份证号</th> </tr> <c:forEach items="${page.dataList }" var="user"> <tr> <td>${user.id }</td> <td>${user.name }</td> <td>${user.pwd }</td> <td>${user.idCard }</td> </tr> </c:forEach> </table> <span>第${page.currPage }页/共${page.totalPage }页</span> <br> <br> <form action="showUserList" method="get"> <input type="submit" name="operation" value="首页"> <input type="submit" name="operation" value="上一页"> <input type="submit" name="operation" value="下一页"> <input type="submit" name="operation" value="尾页"> <input type="hidden" name="currPage" value="${page.currPage }"> </form></body></html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章