JavaWeb分頁的實(shí)現(xiàn)代碼實(shí)例
這篇文章主要介紹了JavaWeb分頁的實(shí)現(xiàn)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
分頁的分類
分頁的實(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<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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:簡單了解Spring中BeanFactory與FactoryBean的區(qū)別
欄 目:Java
下一篇:詳解IDEA JUnit5測試套件運(yùn)行錯誤的問題
本文標(biāo)題:JavaWeb分頁的實(shí)現(xiàn)代碼實(shí)例
本文地址:http://www.jygsgssxh.com/a1/Java/8880.html
您可能感興趣的文章
- 01-10Springboot中@Value的使用詳解
- 01-10JavaWeb實(shí)現(xiàn)郵件發(fā)送功能
- 01-10淺談Java中真的只有值傳遞么
- 01-10如何解決線程太多導(dǎo)致java socket連接池出現(xiàn)的問題
- 01-10java實(shí)現(xiàn)的順時針/逆時針打印矩陣操作示例
- 01-10java判斷是否空最簡單的方法
- 01-10java郵件亂碼的徹底解決方案
- 01-10JAVA8獨(dú)有的map遍歷方式(非常好用)
- 01-10mybatis分頁絕對路徑寫法過程詳解
- 01-10Java實(shí)現(xiàn)雙保險線程的示例代碼


閱讀排行
本欄相關(guān)
- 01-10Java實(shí)現(xiàn)動態(tài)模擬時鐘
- 01-10Springboot中@Value的使用詳解
- 01-10JavaWeb實(shí)現(xiàn)郵件發(fā)送功能
- 01-10利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能
- 01-10Java實(shí)現(xiàn)動態(tài)數(shù)字時鐘
- 01-10java基于poi導(dǎo)出excel透視表代碼實(shí)例
- 01-10java實(shí)現(xiàn)液晶數(shù)字字體顯示當(dāng)前時間
- 01-10基于Java驗(yàn)證jwt token代碼實(shí)例
- 01-10Java動態(tài)顯示當(dāng)前日期和時間
- 01-10淺談Java中真的只有值傳遞么
隨機(jī)閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10delphi制作wav文件的方法
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實(shí)例總結(jié)


