怎么在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>
关于怎么在JavaWeb中实现分页问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。