怎么在JavaWeb中實(shí)現(xiàn)分頁?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
雄縣ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書銷售渠道,可以享受市場價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!
分頁的分類
分頁的實(shí)現(xiàn)分為真分頁和假分頁兩種。
1.真分頁(物理分頁):
實(shí)現(xiàn)原理: SELECT * FROM xxx [WHERE...] LIMIT ?, 10;
第一個參數(shù)是開始數(shù)據(jù)的索引位置
10是要查詢多少條數(shù)據(jù),即每頁顯示的條數(shù)
優(yōu)點(diǎn): 不會造成內(nèi)存溢出
缺點(diǎn): 翻頁的速度比較慢
2.假分頁(邏輯分頁):
實(shí)現(xiàn)原理: 一次性將所有的數(shù)據(jù)查詢出來放在內(nèi)存之中,每次需要查詢的時候就直接從內(nèi)存之中去取出相應(yīng)索引區(qū)間的數(shù)據(jù)
優(yōu)點(diǎn): 分頁的速度比較快
缺點(diǎn): 可能造成內(nèi)存溢出
分頁的一些術(shù)語:
-- 數(shù)據(jù)總條數(shù): totalCount : select count(1) from t_user;
-- 每頁顯示條數(shù):pageSize
-- 總頁數(shù):totalPage
-- 當(dāng)前頁:currPage
-- 起始索引: startIndex
-- 通過當(dāng)前頁碼查詢第幾頁的數(shù)據(jù)
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
-- 計(jì)算一共有多少頁
-- 方法一:result = totalCount%pageSize,如果余數(shù)result為0,
-- totalPage = totalCount / pageSize
-- 如果余數(shù)result不為0,
-- totalPage = totalCount / pageSize + 1;
-- 方法二:totalPage = (totalCount + pageSize - 1) / pageSize
Pageing工具類
public class PaginationBean{ private List dataList; private int currPage; private int totalPage; public List getDataList() { return dataList; } public void setDataList(List 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; } ListuserList = userSevice.getUserListByCurrPage(currPage); PaginationBean pageBean = new PaginationBean (); 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" %>Insert title here
ID | 姓名 | 密碼 | 身份證號 |
---|---|---|---|
${user.id } | ${user.name } | ${user.pwd } | ${user.idCard } |
關(guān)于怎么在JavaWeb中實(shí)現(xiàn)分頁問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。