分页原生代码示例

1 分析 需要五个数据

​ 商品数据 List ------> 数据库查询 limit 每页个数

​ 当前第几页----》用户点击页码传递 第一次开页面 默认页码 为1

​ 商品的总数量----》 count()

​ 共有多少页:总数量/ 每页个数 向上取整

​ 每页显示多少个 12

​ 五个数据封装成一个对象 PageBean

2 创建PageBean对象

//  当前页数
    private int currentPage;
    // 总数量
    private long totalCount;
    // 总页数
    private  int totalPage;
    //  每页显示的条数
    private int pageSize;
    // 分页中的数据  集合的数据类型  写成泛型
    private List<T> list;

3 找到页面 product_list.jsp

<!--分页 -->
<div style="width: 380px; margin: 0 auto; margin-top: 50px;">
   <ul class="pagination" style="text-align: center; margin-top: 10px;">
      <!--上一页   当前页减去1  如果是第一页  不能点击-->
      <c:if test="${pb.currentPage==1}">
         <li class="disabled"><a href="#" aria-label="Previous"><span
               aria-hidden="true">&laquo;</span></a></li>
      </c:if>
      <c:if test="${pb.currentPage!=1}">
         <li><a href="${pageContext.request.contextPath}/page?currentPage=${pb.currentPage-1}" aria-label="Previous"><span
               aria-hidden="true">&laquo;</span></a></li>
      </c:if>
       <!--页码不是死的  而是循环出来的   开始循环的变量是1  循环结束  总页数-->
      <c:forEach begin="1" end="${pb.totalPage}" var="i">
         <!--当前页码 页码不能点击  背景样式-->
         <c:if test="${i==pb.currentPage}">
            <li class="active"><a>${i}</a></li>
         </c:if>
         <c:if test="${i!=pb.currentPage}">
            <li><a href="${pageContext.request.contextPath}/page?currentPage=${i}">${i}</a></li>
         </c:if>
      </c:forEach>
      <!--下一页   当前页+1  如果是最后一页  不能点击-->
      <c:if test="${pb.currentPage==pb.totalPage}">
         <li class="disabled"><a aria-label="Next"><span
               aria-hidden="true">&raquo;</span></a></li>
      </c:if>
      <c:if test="${pb.currentPage!=pb.totalPage}">
         <li><a href="${pageContext.request.contextPath}/page?currentPage=${pb.currentPage+1}" aria-label="Next"><span
               aria-hidden="true">&raquo;</span></a></li>
      </c:if>

      </a></li>
   </ul>
</div>
<!-- 分页结束 -->

4 编写servlet

package cn.lijun.web;

import cn.lijun.domain.PageBean;
import cn.lijun.domain.Product;
import cn.lijun.service.ProductService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author lijun
 * @date 2019/7/25 15:21
 */
@WebServlet(urlPatterns = "/page")
public class PageServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //   获取客户端提交的当前页   当前页传递Service    接受业务层返回的PageBean数据
        //   存储request域中   转发页面
        String currentPage = request.getParameter("currentPage");
        if(currentPage==null){
            currentPage="1";
        }
        //  调用业务层  传递当前页
        ProductService service = new ProductService();
        PageBean<Product> pageBean = service.getPageBean(Integer.parseInt(currentPage));
        request.setAttribute("pb",pageBean);
        request.getRequestDispatcher("/product_list.jsp").forward(request,response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }


}

5 编写service

//  分页
    public PageBean<Product> getPageBean(int currentPage) {
        PageBean<Product> pb = new PageBean<Product>();
        //  封装当前页
        pb.setCurrentPage(currentPage);
        //  封装每页个数
        pb.setPageSize(12);
        // 封装需要的商品数据   dao 层查询   需要当前页和每页条数
        List<Product> list = null;
        try {
            list = dao.findByPage(currentPage,12);

        pb.setList(list);
        // 封装总数量  从dao 获得
        long totalCount = dao.getTotalCount();
        pb.setTotalCount(totalCount);
        // 封装总页数
        int totalPage = (int)Math.ceil((totalCount*1.0/12));
        pb.setTotalPage(totalPage);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pb;

    }

6 编写 dao

 // 获取所有商品的总量
    public long getTotalCount() throws SQLException {
        String sql = "select count(*) from product";
        return qr.query(sql,new ScalarHandler<Long>());
    }
    // 分页 获得分页查询的商品
    public List<Product> findByPage(int currentPage, int pageSize) throws SQLException {
        //   sql
        String sql="select * from product limit ?,? ";
        return qr.query(sql,new BeanListHandler<Product>(Product.class),(currentPage-1)*pageSize,pageSize);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值