0608-web:分頁

主要內(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>&nbsp;&nbsp;
                    <span>總記錄數(shù):${requestScope.pagemsg.totalCount }&nbsp;&nbsp;每頁顯示:${requestScope.pagemsg.pageSize}</span>&nbsp;&nbsp;
                    <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>&nbsp;&nbsp;
                    <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>&nbsp;&nbsp;
           <%--點擊上一頁,currentPage屬性會-1,即輸入controller層中輸入的參數(shù)currentPage會-1,從而實現(xiàn)上一頁的功能--%>
           <a href="${pageContext.request.contextPath }/task2/list?currentPage=${requestScope.pagemsg.currentPage-1}">[上一頁]</a>&nbsp;&nbsp;
       </c:if>
                        <%--避免在最后一頁可以點擊下一頁--%>
       <c:if test="${requestScope.pagemsg.currentPage != requestScope.pagemsg.totalPage}">
           <a href="${pageContext.request.contextPath }/task2/list?currentPage=${requestScope.pagemsg.currentPage+1}">[下一頁]</a>&nbsp;&nbsp;
           <a href="${pageContext.request.contextPath }/task2/list?currentPage=${requestScope.pagemsg.totalPage}">[尾頁]</a>&nbsp;&nbsp;
       </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;
    }
    
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容