JSTL分页

1.jsp页面代码:
    <!-- 分页开始 -->
    <div class="paging">
     <ul>
      <li class="click">
       <c:if test="${currentPage>1}">
        <a href="#">首页</a>
       </c:if>
      </li>
      <li class="click">
       <c:if test="${currentPage>1}">
        <a href="#">上一页</a>
       </c:if>
      </li>
      <c:if test="${countPage<=6}">
       <c:forEach var="i" begin="1" end="${countPage}">
        <c:if test="${i==currentPage}">
         <li class="click">
          ${i}
         </li>
        </c:if>
        <c:if test="${i!=currentPage}">
         <li>
          <a
           href="#">${i}</a>
         </li>
        </c:if>
       </c:forEach>
      </c:if>

      <c:if test="${countPage>6}">
       <c:if test="${currentPage<=3}">

        <c:forEach var="i" begin="1" end="${currentPage+1}">
         <c:if test="${i==currentPage}">
          <li class="click">
           ${i}
          </li>
         </c:if>
         <c:if test="${i!=currentPage}">
          <li>
           <a href="#">${i}</a>
          </li>
         </c:if>
        </c:forEach>
        <li class="click">
         ...
        </li>
        <c:forEach var="i" begin="${countPage-1}" end="${countPage}">
         <li>
          <a href="#">${i}</a>
         </li>
        </c:forEach>
       </c:if>


       <c:if test="${currentPage>3}">

        <c:if test="${currentPage+6>countPage}">

         <c:if test="${currentPage==4}">
          <c:forEach var="i" begin="1" end="2">
           <li>
            <a href="#">${i}</a>
           </li>
          </c:forEach>
         </c:if>
         <c:if test="${currentPage!=4}">
          <c:forEach var="i" begin="1" end="3">
           <li>
            <a href="#">${i}</a>
           </li>
          </c:forEach>
         </c:if>

         <li class="click">
          ...
         </li>
         <c:forEach var="i" begin="${currentPage-1}" end="${countPage}">

          <c:if test="${i==currentPage}">
           <li class="click">
            ${i}
           </li>
          </c:if>
          <c:if test="${i!=currentPage}">
           <li>
            <a href="#">${i}</a>
           </li>
          </c:if>

         </c:forEach>
        </c:if>

        <c:if test="${currentPage+6<=countPage}">
         <c:if test="${currentPage==4}">
          <c:forEach var="i" begin="1" end="2">
           <li>
            <a href="#">${i}</a>
           </li>
          </c:forEach>
         </c:if>
         <c:if test="${currentPage!=4}">
          <c:forEach var="i" begin="1" end="3">
           <li>
            <a href="#">${i}</a>
           </li>
          </c:forEach>
         </c:if>
         <li class="click">
          ...
         </li>
         <c:forEach var="i" begin="${currentPage-1}"
          end="${currentPage+3}">

          <c:if test="${i==currentPage}">
           <li class="click">
            ${i}
           </li>
          </c:if>
          <c:if test="${i!=currentPage}">
           <li>
            <a href="#">${i}</a>
           </li>
          </c:if>

         </c:forEach>
         <li class="click">
          ...
         </li>
         <c:forEach var="i" begin="${countPage-1}" end="${countPage}">
          <li>
           <a href="#">${i}</a>
          </li>
         </c:forEach>
        </c:if>
       </c:if>
      </c:if>
      <c:if test="${currentPage<countPage}">
       <li class="click">
        <a href="#">下一页</a>
       </li>
      </c:if>

      <c:if test="${currentPage<countPage}">
       <li class="click">
        <a href="#">末页</a>
       </li>
      </c:if>
     </ul>
    </div>
    <!-- 分页结束 -->
     
    
    
2.Action代码:
 //当前页面
 private int currentPage = 1;

 //页面大小
 private int pageSize = 15;

 //总页数
 static int countPage = 1;

 //countPage的getter和setter方法
 public int getCurrentPage() {
  return currentPage;
 }
 public void setCurrentPage(int currentPage) {
  this.currentPage = currentPage;
 }
 
 
 //execute()中的代码
  //查询总页数
  countPage = DAO对象.pageCount(表名,pageSize, 数据库连接);
 
  request.setAttribute("countPage", countPage);
  request.setAttribute("currentPage", currentPage);


3.DAO代码
 
 public int pageCount(String tableName, int pageSize,
   DataBaseService dataBaseService) {

  int count = dataBaseService.findRows("select count(*) from "
    + tableName);

  return count / pageSize + 1;
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值