JavaWeb 簡單分頁實現(xiàn)代碼
JavaWeb 簡單分頁的實現(xiàn):
這次主要是講解一下通過登錄后對得到的數(shù)據(jù)進行分頁,首先我們新建一個登錄頁面login.jsp,因為我們主要學(xué)習(xí)一下分頁,所以登錄驗證的部分不再闡述,主要代碼如下:
<form action="pageServlet"> 用戶名:<input type="text" name="username"><br> 密 碼:<input type="text" name="password"><br> <input type="submit" value="提交"> </form>
首先建立實體類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ù),同時做一些分頁的準備,具體含義可以參照注釋理解,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ù)進行測試效果,小伙伴可以連接數(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); //獲取當前頁的頁數(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; // 在實際開發(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表達式獲取得到,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)容進行獲取。注意,這里我默認是每頁兩條數(shù)據(jù),所以是(pageNos-1)*2,如果每頁N條數(shù)據(jù)則需將2改為N,當然N也可以從后臺Servlet中獲取得到。
同時,因為我們在index.jsp中用了JSTL表達式,所以記得要導(dǎo)入引用:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
到這里我們就完成了一個簡單的分頁,快去試試吧。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
詳解JAVA中ListIterator和Iterator的辨析
這篇文章主要為大家詳細介紹了JAVAListIterator和Iterator的辨析,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02解決Swagger2返回map復(fù)雜結(jié)構(gòu)不能解析的問題
這篇文章主要介紹了解決Swagger2返回map復(fù)雜結(jié)構(gòu)不能解析的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07使用eclipse + maven一步步搭建SSM框架教程詳解
SSM(Spring+SpringMVC+MyBatis)框架集由Spring、SpringMVC、MyBatis三個開源框架整合而成,常作為數(shù)據(jù)源較簡單的web項目的框架.這篇文章主要介紹了eclipse + maven搭建SSM框架 ,需要的朋友可以參考下2017-11-11SpringBoot項目訪問任意接口出現(xiàn)401錯誤的解決方案
今天小編就為大家分享一篇關(guān)于SpringBoot項目訪問任意接口出現(xiàn)401錯誤的解決方案,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01