美文网首页
0608-web:分页

0608-web:分页

作者: 小咕咕coco | 来源:发表于2020-06-08 23:15 被阅读0次

    主要内容:

    • 一个page实体类:用于保存一些变量,比如总的记录数量,一页显示多少记录,返回的每页项目list
    • 分页对应的service方法:页号为参数,返回该页的item集合(本质上是确定首位+一个limit语句的调用)
    • 页脚的标签:首页尾页上下页号等:标注连接+更新参数
                  <tr bgcolor="#add8e6">
                      <%--合并横向单元格,使用td样式--%>
                  <td colspan="5" class="td">
                          <%--显示当前页数,requestScope.pagemsg.currentPage等价于request.getAttribute(pagemsg.currentPage)等价于${pagemsg.currentPage}--%>
                      <span>第${requestScope.pagemsg.currentPage }/ ${requestScope.pagemsg.totalPage}页</span>&nbsp;&nbsp;
                      <span>总记录数:${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="跳转到">
                          <input type="submit" value="跳转">
                      </form>
                  </span>&nbsp;&nbsp;
                      <span>
                      <%--利用此判断可以杜绝第一页还可以点击到上一页的情况,也可以在controller中设置限制条件--%>
         <c:if test="${requestScope.pagemsg.currentPage != 1}">
             <%--默认出现第一页,点击首页按钮会跳转到第一页--%>
             <a href="${pageContext.request.contextPath }/task2/list?currentPage=1">[首页]</a>&nbsp;&nbsp;
             <%--点击上一页,currentPage属性会-1,即输入controller层中输入的参数currentPage会-1,从而实现上一页的功能--%>
             <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中根据参数调用service方法即可,返回值传送到view层,jsp中for each遍历列出
      //currentPage这个值是从jsp页面传回来的,对应不同的按钮值也不同,根据传回来的不同值传进不同的值,默认为1,即没有按任何键的情况下自动显示第一页
      public ModelAndView listPersonByPage(@RequestParam(value = "currentPage" ,defaultValue="1",required = false)int currentPage){
          ModelAndView mav=new ModelAndView();
      //使用mav将查找到的数据传到list.jsp里,再在jsp中动态获取数据
          mav.addObject("pagemsg",personService.listByPage(currentPage));
      //注意此时的attributeName为pagemsg,后面会使用到其来选定属性和路径
          mav.setViewName("list");
      //设置返回的视图页面为list.jsp,前缀后缀在spring-mvc.xml中设置了
          return mav;
      }
      

    相关文章

      网友评论

          本文标题:0608-web:分页

          本文链接:https://www.haomeiwen.com/subject/kxfjtktx.html