主要內(nèi)容:
- 一個page實體類:用于保存一些變量,比如總的記錄數(shù)量,一頁顯示多少記錄,返回的每頁項目list
- 分頁對應(yīng)的service方法:頁號為參數(shù),返回該頁的item集合(本質(zhì)上是確定首位+一個limit語句的調(diào)用)
- 頁腳的標(biāo)簽:首頁尾頁上下頁號等:標(biāo)注連接+更新參數(shù)
<tr bgcolor="#add8e6"> <%--合并橫向單元格,使用td樣式--%> <td colspan="5" class="td"> <%--顯示當(dāng)前頁數(shù),requestScope.pagemsg.currentPage等價于request.getAttribute(pagemsg.currentPage)等價于${pagemsg.currentPage}--%> <span>第${requestScope.pagemsg.currentPage }/ ${requestScope.pagemsg.totalPage}頁</span> <span>總記錄數(shù):${requestScope.pagemsg.totalCount } 每頁顯示:${requestScope.pagemsg.pageSize}</span> <span> <form style="display: inline" action="/task2/list"> <input type="text" name="currentPage" size="5" placeholder="跳轉(zhuǎn)到"> <input type="submit" value="跳轉(zhuǎn)"> </form> </span> <span> <%--利用此判斷可以杜絕第一頁還可以點擊到上一頁的情況,也可以在controller中設(shè)置限制條件--%> <c:if test="${requestScope.pagemsg.currentPage != 1}"> <%--默認(rèn)出現(xiàn)第一頁,點擊首頁按鈕會跳轉(zhuǎn)到第一頁--%> <a href="${pageContext.request.contextPath }/task2/list?currentPage=1">[首頁]</a> <%--點擊上一頁,currentPage屬性會-1,即輸入controller層中輸入的參數(shù)currentPage會-1,從而實現(xiàn)上一頁的功能--%> <a href="${pageContext.request.contextPath }/task2/list?currentPage=${requestScope.pagemsg.currentPage-1}">[上一頁]</a> </c:if> <%--避免在最后一頁可以點擊下一頁--%> <c:if test="${requestScope.pagemsg.currentPage != requestScope.pagemsg.totalPage}"> <a href="${pageContext.request.contextPath }/task2/list?currentPage=${requestScope.pagemsg.currentPage+1}">[下一頁]</a> <a href="${pageContext.request.contextPath }/task2/list?currentPage=${requestScope.pagemsg.totalPage}">[尾頁]</a> </c:if> - controller和jsp頁面:con中根據(jù)參數(shù)調(diào)用service方法即可,返回值傳送到view層,jsp中for each遍歷列出
//currentPage這個值是從jsp頁面?zhèn)骰貋淼?,對?yīng)不同的按鈕值也不同,根據(jù)傳回來的不同值傳進不同的值,默認(rèn)為1,即沒有按任何鍵的情況下自動顯示第一頁 public ModelAndView listPersonByPage(@RequestParam(value = "currentPage" ,defaultValue="1",required = false)int currentPage){ ModelAndView mav=new ModelAndView(); //使用mav將查找到的數(shù)據(jù)傳到list.jsp里,再在jsp中動態(tài)獲取數(shù)據(jù) mav.addObject("pagemsg",personService.listByPage(currentPage)); //注意此時的attributeName為pagemsg,后面會使用到其來選定屬性和路徑 mav.setViewName("list"); //設(shè)置返回的視圖頁面為list.jsp,前綴后綴在spring-mvc.xml中設(shè)置了 return mav; }