JavaWeb分頁的實現(xiàn)代碼實例
這篇文章主要介紹了JavaWeb分頁的實現(xiàn)代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
分頁的分類
分頁的實現(xiàn)分為真分頁和假分頁兩種。
1.真分頁(物理分頁):
實現(xiàn)原理: SELECT * FROM xxx [WHERE...] LIMIT ?, 10;
第一個參數(shù)是開始數(shù)據(jù)的索引位置
10是要查詢多少條數(shù)據(jù),即每頁顯示的條數(shù)
優(yōu)點: 不會造成內(nèi)存溢出
缺點: 翻頁的速度比較慢
2.假分頁(邏輯分頁):
實現(xiàn)原理: 一次性將所有的數(shù)據(jù)查詢出來放在內(nèi)存之中,每次需要查詢的時候就直接從內(nèi)存之中去取出相應索引區(qū)間的數(shù)據(jù)
優(yōu)點: 分頁的速度比較快
缺點: 可能造成內(nèi)存溢出
分頁的一些術語:
- -- 數(shù)據(jù)總條數(shù): totalCount : select count(1) from t_user;
- -- 每頁顯示條數(shù):pageSize
- -- 總頁數(shù):totalPage
- -- 當前頁:currPage
- -- 起始索引: startIndex
-- 通過當前頁碼查詢第幾頁的數(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
-- 計算一共有多少頁
-- 方法一: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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
java并發(fā)編程關鍵字volatile保證可見性不保證原子性詳解
這篇文章主要為大家介紹了java并發(fā)編程關鍵字volatile保證可見性不保證原子性詳解,文中附含詳細示例說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-02-02
SpringBoot集成WebSocket的兩種方式(JDK內(nèi)置版和Spring封裝版)
這篇文章主要介紹了SpringBoot集成WebSocket的兩種方式,這兩種方式為JDK內(nèi)置版和Spring封裝版,本文結合示例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-06-06
使用Idea或Datagrip導入excel數(shù)據(jù)的方法
這篇文章主要介紹了使用Idea或Datagrip導入excel數(shù)據(jù)的方法,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
springcloud整合到項目中無法啟動報錯Failed to start bean&n
這篇文章主要介紹了springcloud整合到項目中無法啟動報錯Failed to start bean 'eurekaAutoServiceRegistration'問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01

