商品的分页显示

第一次进去得显示第一页
<a href="/Web13_1/PageProductServlet?currPage=1">分页显示</a>
PageProductServlet:
public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        try {
            //接收当前页数
            int currPage = Integer.parseInt(request.getParameter("currPage"));
            ProductService productService = new ProductService();
            //调用业务层处理数据
            PageBean pageBean = productService.pageShow(currPage);
            //页面跳转
            request.setAttribute("pageBean", pageBean);
            request.getRequestDispatcher("/product/product_page.jsp").forward(request, response);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
ProductService:
/**
     * 分页显示
     * @param currPage
     * @return
     * @throws SQLException 
     */
    public PageBean pageShow(int currPage) throws SQLException {
        /**
         * 设置PageBean数据
         */
        ProductDao productDao = new ProductDao();
        PageBean pageBean = new PageBean();
        //设置当前页数
        pageBean.setCurrPage(currPage);
        //每页显示记录数
        int pageSize = 10;
        pageBean.setPageSize(10);
        //设置总记录数
        int totalCount = productDao.findTotalCount();
        pageBean.setTotalCount(totalCount);
        //设置总页数
        int totalPage = 0;
        if(totalCount % pageSize == 0) {
            totalPage = totalCount/pageSize;
        }
        if(totalCount % pageSize != 0) {
            totalPage = totalCount/pageSize + 1;
        }
        /*
        //把totalCount转为double类型
        double tC = totalCount;
        //向上取整
        Double num = Math.ceil(tC/pageSize);
        //将totalPage转为int型
        totalPage = num.intValue();
        */
        pageBean.setTotalPage(totalPage);
        //设置每页的数据集合
        int begin = (currPage-1)*pageSize;
        List<Product> list = productDao.findPageProduct(begin,pageSize);
        pageBean.setList(list);
        return pageBean;
    }
ProductDao:
   /**
     * 查询总记录数
     * @return
     * @throws SQLException 
     */
    public int findTotalCount() throws SQLException {
        QueryRunner queryRunner = new QueryRunner(C3P0JDBCUtils.getDataSource());
        String sql = "select count(*) from product";
        Long totalCount = queryRunner.query(sql, new ScalarHandler());  
        return totalCount.intValue();
    }

    /**
     * 查询每页的数据
     * @param begin
     * @param pageSize
     * @return
     * @throws SQLException
     */
    public List<Product> findPageProduct(int begin, int pageSize) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(C3P0JDBCUtils.getDataSource());
        String sql = "select * from product order by pdate desc limit ?,?";
        List<Product> list = queryRunner.query(sql, new BeanListHandler<Product>(Product.class), begin,pageSize);
        return list;
    }
Product_page.jsp:
<tr>
            <td colspan="8" align="center">
                第${ pageBean.currPage }/${ pageBean.totalPage }页 &nbsp;&nbsp;
                共${ pageBean.totalCount }个商品&nbsp;&nbsp;
                每页显示${ pageBean.pageSize }个商品&nbsp;&nbsp;

                <!-- 加个判断,页数为1时,首页和上一页不显示 -->
                <c:if test="${ pageBean.currPage != 1 }">
                    <a href="${ pageContext.request.contextPath }/PageProductServlet?currPage=1">首页</a>
                    <a href="${ pageContext.request.contextPath }/PageProductServlet?currPage=${ pageBean.currPage - 1 }">上一页</a>
                </c:if>

                <!-- 显示对应页数的数字连接,当页数为当前页时,当前页的数字无链接效果不可点击 -->
                <c:forEach var="page" begin="1" end="${ pageBean.totalPage }">
                    <c:if test="${ pageBean.currPage == page }">
                        ${ page }
                    </c:if> 
                    <c:if test="${ pageBean.currPage != page }">
                        <a href="${ pageContext.request.contextPath }/PageProductServlet?currPage=${ page }">${ page }</a>
                    </c:if>
                </c:forEach>

                <!-- 加个判断,页数为尾页时,尾页和下一页不显示 -->
                <c:if test="${ pageBean.currPage != pageBean.totalPage }">
                    <a href="${ pageContext.request.contextPath }/PageProductServlet?currPage=${ pageBean.currPage + 1 }">下一页</a>
                    <a href="${ pageContext.request.contextPath }/PageProductServlet?currPage=${ pageBean.totalPage }">尾页</a>
                </c:if>
            </td>
        </tr>

猜你喜欢

转载自blog.csdn.net/sinat_40662281/article/details/80456201