(十三)分页展示商品

Posted Michael2397

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(十三)分页展示商品相关的知识,希望对你有一定的参考价值。

    按类别 分页展示
    步骤分析:
        在菜单上 点击一个分类 head.jsp
            <a href="/store/product?method=findByPage&cid=${}&currPage=1"></a>
        findByPage操作:
            1.接受 cid   currPage  设定一个每页显示的条数  pageSize
            2.调用productSerivce 返回一个PageBean
                pageBean中
                    list  currPage pageSize totalPage totalCount
            3.将pagebean放入request域中,请求转发
        在productSerivce需要封装成pagebean
        
        在product_list.jsp展示数据

com.louis.domain.PageBean<E>

package com.louis.domain;

import java.util.List;

public class PageBean<E> {
    private List<E> list;
    private Integer currPage;
    private Integer pageSize;
    private Integer totalPage;
    private Integer totalCount;
    public List<E> getList() {
        return list;
    }
    public void setList(List<E> list) {
        this.list = list;
    }
    public Integer getCurrPage() {
        return currPage;
    }
    public void setCurrPage(Integer currPage) {
        this.currPage = currPage;
    }
    public Integer getPageSize() {
        return pageSize;
    }
    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }
    public Integer getTotalPage() {
        return (int)Math.ceil(totalCount*1.0/pageSize);
    }
    
    public Integer getTotalCount() {
        return totalCount;
    }
    public void setTotalCount(Integer totalCount) {
        this.totalCount = totalCount;
    }
    public PageBean() { }
    
    public PageBean(List<E> list, Integer currPage, Integer pageSize, Integer totalCount) {
        super();
        this.list = list;
        this.currPage = currPage;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
    }
    
    
    
}

前端head.jsp

<script>
    $(function() {
        //发送ajax请求
        $.get("${pageContext.request.contextPath}/category?method=findAll",function(data){
            //获取元素
            var $ul = $("#menuId");
            //遍历数组
            $(data).each(function(){
                $ul.append($("<li><a href=‘${pageContext.request.contextPath}/product?method=findByPage&cid="+this.cid+"&currPage=1‘>"+this.cname+"</a></li>"));
            });
            
        },"json");
    });
</script>

com.louis.web.servlet.ProductServlet

    /*
     * 分页查询数据
     * */
    public String  findByPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1、获取类别,当前页,设置pageSize
        String cid = request.getParameter("cid");
        int currPage = Integer.parseInt(request.getParameter("currPage"));
        int pageSize = 12;
        
        //2、调用service返回值pageBean
        ProductService productService = new ProductServiceImpl();
        PageBean<Product> pageBean = productService.getByPage(currPage,pageSize,cid);
        
        //3、将结果放入request中请求转发
        request.setAttribute("pb", pageBean);
        return "/jsp/product_info.jsp";
    }

com.louis.service.impl.ProductServiceImpl

    //按类别查询商品
    @Override
    public PageBean<Product> getByPage(int currPage, int pageSize, String cid) throws Exception {
        ProductDao productDao = new ProductDaoImpl();
        //当前页数据
        List<Product> list = productDao.findByPage(currPage,pageSize,cid);
        
        //总条数
        int totalCoun = productDao.getTotalCount(cid);
        
        return new PageBean<>(list,currPage,pageSize,totalCoun);
    }

com.louis.dao.impl.ProductDaoImpl

    /**
     * 查询当前也需要展示的数据
     */
    @Override
    public List<Product> findByPage(int currPage, int pageSize, String cid) throws Exception {
        QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select * from product where cid = ? limit ?,?";
        return qr.query(sql, new BeanListHandler<>(Product.class), cid,(currPage-1)*pageSize,pageSize);
    }
    /**
     * 查询当前类别的总条数
     */
    @Override
    public int getTotalCount(String cid) throws Exception {
        QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
        String sql="select count(*) from product where cid = ?";
        return ((Long)qr.query(sql, new ScalarHandler(), cid)).intValue();
    }

 

 

问题

return ((Long)qr.query(sql, new ScalarHandler(), cid)).intValue();

以上是关于(十三)分页展示商品的主要内容,如果未能解决你的问题,请参考以下文章

Android开发案例 - 淘宝商品详情

请 php 简单 产品分类代码

[项目] 电子商城项目 Part.2

商品的增删改查

校园商铺-8商品模块-10商品列表展示之后端开发

用winform怎么实现图片列表展示,类似于淘宝网上成列的商品一样,可以上下翻页