JavaWeb 簡單分頁實(shí)現(xiàn)代碼
JavaWeb 簡單分頁的實(shí)現(xiàn):
這次主要是講解一下通過登錄后對得到的數(shù)據(jù)進(jìn)行分頁,首先我們新建一個登錄頁面login.jsp,因?yàn)槲覀冎饕獙W(xué)習(xí)一下分頁,所以登錄驗(yàn)證的部分不再闡述,主要代碼如下:
<form action="pageServlet">
用戶名:<input type="text" name="username"><br>
密 碼:<input type="text" name="password"><br>
<input type="submit" value="提交">
</form>
首先建立實(shí)體類User.java并添加get和set方法:
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
我們可以看到form表單是提交到pageServlet中,所以我們新建一個PageServlet,并在Servlet中獲取到數(shù)據(jù),同時做一些分頁的準(zhǔn)備,具體含義可以參照注釋理解,PageServlet代碼:
public class PageServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<User> list = new ArrayList<User>();
// 在這里我不再連接數(shù)據(jù)庫而是用虛擬的數(shù)據(jù)進(jìn)行測試效果,小伙伴可以連接數(shù)據(jù)庫查詢到之后返回一個list
for (int i = 1; i < 7; i++) {
User user1 = new User();
user1.setUsername("第" + i + "個用戶名");
user1.setPassword("第" + i + "密碼");
list.add(user1);
}
HttpSession session = request.getSession();
// 將數(shù)據(jù)存到session中以便于在前臺獲取
session.setAttribute("userList", list);
//獲取當(dāng)前頁的頁數(shù)并轉(zhuǎn)為int類型,最終將數(shù)據(jù)存到session中
int pageNos;
if (request.getParameter("pageNos") == null
|| Integer.parseInt(request.getParameter("pageNos")) < 1) {
pageNos = 1;
} else {
pageNos = Integer.parseInt(request.getParameter("pageNos"));
}
session.setAttribute("pageNos", pageNos);
// 定義總頁數(shù)并存到session中
int countPage = 3;
// 在實(shí)際開發(fā)中我們的總頁數(shù)可以根據(jù)sql語句得到查詢到的總條數(shù),然后用總條數(shù)除每頁的條數(shù)得到總頁數(shù)
session.setAttribute("countPage", countPage);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
在上述代碼中我們最終將轉(zhuǎn)發(fā)到index.jsp頁面,此時我們所有的數(shù)據(jù)都將顯示在index.jsp中,用JSTL和EL表達(dá)式獲取得到,index.jsp主要代碼如下:
<body>
<c:forEach items="${userList}" var="user" begin="${(pageNos-1)*2 }"
end="${pageNos*2-1}">
<center>
<div>${user.username}</div>
</center>
<center>
<div>${user.password}</div>
</center>
</c:forEach>
<center>
<c:if test="${pageNos>1 }">
<a href="pageServlet?pageNos=1" >首頁</a>
<a href="pageServlet?pageNos=${pageNos-1 }">上一頁</a>
</c:if>
<c:if test="${pageNos <countPage }">
<a href="pageServlet?pageNos=${pageNos+1 }">下一頁</a>
<a href="pageServlet?pageNos=${countPage }">末頁</a>
</c:if>
</center>
<form action="pageServlet">
<h4 align="center">共${countPage}頁
<input type="text" value="${pageNos}" name="pageNos" size="1">頁
<input type="submit" value="go">
</h4>
</form>
</body>
第二行中我們用<c:forEach >對session.setAttribute();中的內(nèi)容進(jìn)行獲取。注意,這里我默認(rèn)是每頁兩條數(shù)據(jù),所以是(pageNos-1)*2,如果每頁N條數(shù)據(jù)則需將2改為N,當(dāng)然N也可以從后臺Servlet中獲取得到。
同時,因?yàn)槲覀冊趇ndex.jsp中用了JSTL表達(dá)式,所以記得要導(dǎo)入引用:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
到這里我們就完成了一個簡單的分頁,快去試試吧。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
解決spring boot hibernate 懶加載的問題
這篇文章主要介紹了解決spring boot hibernate 懶加載的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
java實(shí)現(xiàn)簡單的客戶信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡單的客戶信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-06-06
Java實(shí)現(xiàn)將txt/word/pdf轉(zhuǎn)成圖片并在線預(yù)覽的功能
本文將基于aspose-words(用于txt、word轉(zhuǎn)圖片),pdfbox(用于pdf轉(zhuǎn)圖片),封裝成一個工具類來實(shí)現(xiàn)txt、word、pdf等文件轉(zhuǎn)圖片的需求并實(shí)現(xiàn)在線預(yù)覽功能,需要的可以參考一下2023-05-05
spring boot org.junit.jupiter.api不存在的解決
這篇文章主要介紹了spring boot org.junit.jupiter.api不存在的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
Spring Boot監(jiān)聽Redis Key失效事件實(shí)現(xiàn)定時任務(wù)的示例
這篇文章主要介紹了Spring Boot監(jiān)聽Redis Key失效事件實(shí)現(xiàn)定時任務(wù)的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
Spring中@Import的各種用法以及ImportAware接口詳解
這篇文章主要介紹了Spring中@Import的各種用法以及ImportAware接口詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10

