商品分页操作

1.分页操作主要是封装好一个PageBean

PageBean.java

public class PageBean<T> {
    //当前页
    private int currentPage ;
    //当前页显示的商品条数
    private int currentCount ;
    //总共有多少商品
    private int totalCount ;
    //共分为多少页
    private int totalPage ;
    //当前页显示的商品条数
    private List<T> list ;

}

2.jsp页面

<div class="row" style="width: 1210px; margin: 0 auto;">
        <div class="col-md-12">
            <ol class="breadcrumb">
                <li><a href="#">首页</a></li>
            </ol>
        </div>

        <c:forEach items="${pageBean.list }" var="product">
            <div class="col-md-2" style="height: 250px">
                <a href="${pageContext.request.contextPath }/product?method=ProductInfoServlet&cid=${cid }&pid=${product.pid}&currentPage=${pageBean.currentPage }">
                    <img src="${pageContext.request.contextPath }/${product.pimage }" width="170" height="170" style="display: inline-block;">
                </a>
                <p>
                    <a href="${pageContext.request.contextPath }/product?method=ProductInfoServlet&cid=${cid }&pid=${product.pid}&currentPage=${pageBean.currentPage }" style='color: green'>${product.pname }</a>
                </p>
                <p>
                    <font color="#FF0000">商城价:&yen;${product.shop_price }</font>
                </p>
            </div>
        </c:forEach>

    </div>

<!--分页 -->
    <div style="width: 380px; margin: 0 auto; margin-top: 50px;">
        <ul class="pagination" style="text-align: center; margin-top: 10px;">
        <!-- 左边返回上一页按钮 -->
            <c:if test="${pageBean.currentPage==1 }">
                <li class="disabled"><a href="javascript:;" aria-label="Previous"><span
                    aria-hidden="true">&laquo;</span></a></li>
            </c:if>
            <c:if test="${pageBean.currentPage!=1 }">
                <li>
                <a href="${pageContext.request.contextPath }/product?method=findCategoryInfo&cid=${cid }&currentPage=${pageBean.currentPage-1 }"
                    aria-label="Previous"><span
                    aria-hidden="true">&laquo;</span></a>
                </li>
            </c:if>
            <!-- 主题部分  点击第几页到第几页 -->
            <c:forEach begin="1" end="${pageBean.totalPage }" var="page">
                <c:if test="${pageBean.currentPage==page }">
                    <li class="active">
                        <a href="javascript:;">
                            ${page }
                        </a>
                    </li>
                </c:if>
                <c:if test="${pageBean.currentPage!=page }">
                    <li>
                        <a href="${pageContext.request.contextPath }/product?method=findCategoryInfo&cid=${cid }&currentPage=${page }">
                            ${page }
                        </a>
                    </li>
                </c:if>
            </c:forEach>
            <!-- 右边选择下一页按钮 -->
            <c:if test="${pageBean.currentPage==pageBean.totalPage }">
                <li class="disabled">
                    <a href="javascript:;" aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
            </c:if>
            <c:if test="${pageBean.currentPage!=pageBean.totalPage }">
                <li>
                    <a href="${pageContext.request.contextPath }/product?method=findCategoryInfo&cid=${cid }&currentPage=${pageBean.currentPage+1 }" aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
            </c:if>
        </ul>
    </div>

    <!-- 分页结束 -->

3.servlet

public void ProductListByCidServlet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 接收所点击页数的标志cid
        String cid = request.getParameter("cid");

        // 当前页
        String currentPagestr = request.getParameter("currentPage");
        if (currentPagestr == null) {
            currentPagestr = "1";
        }
        int currentPage = Integer.parseInt(currentPagestr);

        // 当前页显示多少条信息
        int currentCount = 12;

        // 根据cid查询商品列表
        ProductService service = new ProductService();
        PageBean<Product> pageBean = service.findProductBycid(cid, currentPage, currentCount);

        // 存域
        request.setAttribute("pageBean", pageBean);

        request.setAttribute("cid", cid);

        // 转发
        request.getRequestDispatcher("/product_list.jsp").forward(request, response);

    }

4.service层---->用于封装PageBean

//分页分类别查询商品信息
    public PageBean findProductBycid(String cid, int currentPage, int currentCount) {
        
        ProductDao dao = new ProductDao();
        
        //封装PageBean
        PageBean<Product> pageBean = new PageBean<Product>();
        
        //封装currentPage
        pageBean.setCurrentPage(currentPage);
        
        //封装currentCount
        pageBean.setCurrentCount(currentCount);
        
        //封装该类别总共有多少商品TotalCount
        int totalCount = 0  ;
        try {
            totalCount = dao.findProductBycid(cid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        pageBean.setTotalCount(totalCount);
        
        //封装共有多少页
        int totalPage = (int) Math.ceil(1.0*totalCount/currentCount);
        pageBean.setTotalPage(totalPage);
        
        //封装当前页的商品列表
        
        //计算limit的第一个参数
        int index = (currentPage-1)*currentCount;
        List<Product> productListBycid = null ;
        try {
            productListBycid = dao.findProductListBycid(cid,index,currentCount);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        pageBean.setList(productListBycid);
        
        return pageBean;

    }

5.dao层略.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值