(十四)分页展示商品

(十四)分页展示商品

    按类别 分页展示
    步骤分析:
        在菜单上 点击一个分类 head.jsp
            
        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

package com.louis.domain;

import java.util.List;

public class PageBean {
    private List list;
    private Integer currPage;
    private Integer pageSize;
    private Integer totalPage;
    private Integer totalCount;
    public List getList() {
        return list;
    }
    public void setList(List 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 list, Integer currPage, Integer pageSize, Integer totalCount) {
        super();
        this.list = list;
        this.currPage = currPage;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
    }
    
    
    
}

前端head.jsp

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 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 getByPage(int currPage, int pageSize, String cid) throws Exception {
        ProductDao productDao = new ProductDaoImpl();
        //当前页数据
        List 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 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();
    }

 /store/WebContent/jsp/product_list.jsp

            
                
class="col-md-2">

${fn:substring(p.pname,0,10) }...

商城价:¥${p.shop_price }

        
        
    class="pagination" style="text-align:center; margin-top:10px;"> if test="${pb.currPage == 1 }">
  • class="disabled">
  • if> if test="${pb.currPage != 1 }">
  • if>
               begin="${pb.currPage-5>0?pb.currPage-5:1 }" end="${pb.currPage+4>pb.totalPage?pb.totalPage:pb.currPage+4 }" var="n"> if test="${pb.currPage==n }">
  • class="active">${n }
  • if> if test="${pb.currPage!=n }">
  • ${n }
  • if>
    if test="${pb.currPage == pb.totalPage }">
  • class="disabled">
  • if> if test="${pb.currPage != pb.totalPage }">
  • if>

前端展示分类完整代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>



    
        
        
        会员登录
        
        
        
        
        

        
    

    
        
            
    


        
class="row" style="width:1210px;margin:0 auto;">
class="col-md-12">
    class="breadcrumb">
  1. 首页
class="col-md-2">

${fn:substring(p.pname,0,10) }...

商城价:¥${p.shop_price }

    class="pagination" style="text-align:center; margin-top:10px;"> if test="${pb.currPage == 1 }">
  • class="disabled">
  • if> if test="${pb.currPage != 1 }">
  • if> if test="${pb.currPage==n }">
  • class="active">${n }
  • if> if test="${pb.currPage!=n }">
  • ${n }
  • if>
    if test="${pb.currPage == pb.totalPage }">
  • class="disabled">
  • if> if test="${pb.currPage != pb.totalPage }">
  • if>

浏览记录

我们的优势
Copyright © 2005-2016 传智商城 版权所有
View Code

 (十四)分页展示商品_第1张图片

 

问题

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

posted on 2017-10-12 08:59 Michael2397 阅读(...) 评论(...) 编辑 收藏

你可能感兴趣的:((十四)分页展示商品)