0608-web:分頁

主要內(nèi)容:

  • 一個(gè)page實(shí)體類:用于保存一些變量,比如總的記錄數(shù)量,一頁顯示多少記錄,返回的每頁項(xiàng)目list
  • 分頁對應(yīng)的service方法:頁號(hào)為參數(shù),返回該頁的item集合(本質(zhì)上是確定首位+一個(gè)limit語句的調(diào)用)
  • 頁腳的標(biāo)簽:首頁尾頁上下頁號(hào)等:標(biāo)注連接+更新參數(shù)
                <tr bgcolor="#add8e6">
                    <%--合并橫向單元格,使用td樣式--%>
                <td colspan="5" class="td">
                        <%--顯示當(dāng)前頁數(shù),requestScope.pagemsg.currentPage等價(jià)于request.getAttribute(pagemsg.currentPage)等價(jià)于${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>
                    <%--利用此判斷可以杜絕第一頁還可以點(diǎn)擊到上一頁的情況,也可以在controller中設(shè)置限制條件--%>
       <c:if test="${requestScope.pagemsg.currentPage != 1}">
           <%--默認(rèn)出現(xiàn)第一頁,點(diǎn)擊首頁按鈕會(huì)跳轉(zhuǎn)到第一頁--%>
           <a href="${pageContext.request.contextPath }/task2/list?currentPage=1">[首頁]</a>&nbsp;&nbsp;
           <%--點(diǎn)擊上一頁,currentPage屬性會(huì)-1,即輸入controller層中輸入的參數(shù)currentPage會(huì)-1,從而實(shí)現(xiàn)上一頁的功能--%>
           <a href="${pageContext.request.contextPath }/task2/list?currentPage=${requestScope.pagemsg.currentPage-1}">[上一頁]</a>&nbsp;&nbsp;
       </c:if>
                        <%--避免在最后一頁可以點(diǎn)擊下一頁--%>
       <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這個(gè)值是從jsp頁面?zhèn)骰貋淼模瑢?yīng)不同的按鈕值也不同,根據(jù)傳回來的不同值傳進(jìn)不同的值,默認(rèn)為1,即沒有按任何鍵的情況下自動(dòng)顯示第一頁
    public ModelAndView listPersonByPage(@RequestParam(value = "currentPage" ,defaultValue="1",required = false)int currentPage){
        ModelAndView mav=new ModelAndView();
    //使用mav將查找到的數(shù)據(jù)傳到list.jsp里,再在jsp中動(dòng)態(tài)獲取數(shù)據(jù)
        mav.addObject("pagemsg",personService.listByPage(currentPage));
    //注意此時(shí)的attributeName為pagemsg,后面會(huì)使用到其來選定屬性和路徑
        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輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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