JavaWeb分頁的實現程式碼例項
這篇文章主要介紹了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,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>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。