Servlet实现界面分页+过滤

效果图

在这里插入图片描述

分页+过滤逻辑图

在这里插入图片描述

Servlet部分

 protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1.接收请求
//        2.调用方法来处理请求
        ProductQueryObject qb = new ProductQueryObject();
        String strCurrentPage = req.getParameter("currentPage");
        if (StringUtil.hasLength(strCurrentPage)){
            Integer currentPage = Integer.valueOf(strCurrentPage);
            qb.setCurrentPage(currentPage);
        }
        String strPageSize = req.getParameter("pageSize");
        if (StringUtil.hasLength(strPageSize)){
            Integer pageSize = Integer.valueOf(strPageSize);
            qb.setPageSize(pageSize);
        }
        String strProductName = req.getParameter("productName");
        if (StringUtil.hasLength(strProductName)){
            qb.setProductName(strProductName);
        }
        String minSalePrice = req.getParameter("minSalePrice");
        if (StringUtil.hasLength(minSalePrice)){
            qb.setMinSalePrice(new BigDecimal(minSalePrice));
        }
        String maxSalePrice = req.getParameter("maxSalePrice");
        if (StringUtil.hasLength(maxSalePrice)){
            qb.setMaxSalePrice(new BigDecimal(maxSalePrice));
        }
        PageResult result = productService.query(qb);

//        设置共享数据
        req.setAttribute("pageResult",result);
        req.setAttribute("qb",qb);
//        3.控制跳转
        req.getRequestDispatcher("/WEB-INF/views/product/list.jsp").forward(req,resp);
    }

Dao部分

    //查询数量
    int selectForCount(QueryObject qo);

    //查询指定页数据
    List<Product> selectForList(QueryObject qo);

Service部分

public class ProductServiceImpl implements IProductService {
    private IProductDao productDao = new ProductDaoImpl();
    @Override
    public PageResult<Product> query(QueryObject ob) {
        int count = productDao.selectForCount(ob);
        List<Product> products=  productDao.selectForList(ob);
        PageResult<Product> result =new PageResult<>(count,ob.getPageSize(),ob.getCurrentPage(),products);

        return result;
    }
}

封装对象

QueryObject封装对象(分页数据)


@Setter
@Getter
public class QueryObject {
    private int currentPage = 1;
    private int pageSize =5;
//用Limit 子句第一个参数取值使用
    public int getStart(){
        return  (currentPage - 1 ) * pageSize;
    }
}

ProductQueryObject封装对象(过滤数据)

@Getter
@Setter
public class ProductQueryObject extends QueryObject{
    private String productName;
    private BigDecimal minSalePrice;
    private BigDecimal maxSalePrice;

    public String getProductName() {
        if (StringUtil.hasLength(productName)) {
            return productName;
        }
        return null;
    }

}

PageResult封装对象(返回给前端的)

@Getter
@Setter
public class PageResult<T> {

    private int totalCount; //总数量
    private List<T> data; //某页数据

    //3个计算出来的
    private int totalPage; //总页数
    private int prePage;//上一页
    private int nextPage;//下一页

    //用户传递的
    private int currentPage;
    private int pageSize;

    public PageResult(int totalCount,int pageSize , int currentPage,List<T> data ) {
        this.totalCount = totalCount;
        this.data = data;
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.totalPage = totalCount % pageSize ==0 ?  (totalCount / pageSize)  :  (totalCount / pageSize +1);
        this.prePage = currentPage - 1 >= 1 ? currentPage - 1 : 1;
        this.nextPage =  currentPage + 1 <= this.totalPage ? currentPage + 1 : this.totalPage;
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值