JavaWeb实现商品列表的多条件查询和分页功能(超详细的~)




**分页功能实现**



> 分页功能实现思路:  

> 1.输入条件,当前页,每页的数据记录条数,商品集合  

> 2.封装分页类,属性有:pageNumb:当前页,pageSize:每页的数据数,rowCount:总共的商品数据条数,pageCount:总共有多少页,prePageNumb:上一页,nextPageNumb:下一页,currentList:当前页数展示的商品集合,提供有参构造方法,构造Pager类



**Pager类**



public class Pager {

private Integer pageNumb;

private Integer pageSize;



private Integer rowCount;

private Integer pageCount;

private Integer prePageNumb;

private Integer nextPageNumb;

private List currentList;



public Pager(Integer _pageNumb, Integer _pageSize, List _allList){

	//初始化每页的内容条数

	this.pageSize = _pageSize;

	

	//初始化总共的内容条数

	this.rowCount = _allList.size();

	

	//计算共有多少页

	if (this.rowCount % this.pageSize == 0) {

		this.pageCount = this.rowCount / this.pageSize;

	} else {

		this.pageCount = this.rowCount / this.pageSize + 1;

	}

	

	//修正当前页

	if (_pageNumb <= 0) {

		this.pageNumb = 1;

	} else if (_pageNumb > this.pageCount) {

		this.pageNumb = this.pageCount;

	} else {

		this.pageNumb = _pageNumb;

	}

	

	//处理前一页和后一页的页码

	this.prePageNumb = this.pageNumb - 1;

	if (this.prePageNumb <= 0) {

		this.prePageNumb = 1;

	}

	this.nextPageNumb = this.pageNumb + 1;

	if (this.nextPageNumb > this.pageCount) {

		this.nextPageNumb = this.pageCount;

	}

	

	//处理当前页的记录开始结束位置

	Integer fromIndex = (this.pageNumb - 1) * 5;

	Integer toIndex = this.pageNumb * 5;

	if (toIndex > this.rowCount) {

		toIndex = this.rowCount;

	}

	this.currentList = _allList.subList(fromIndex, toIndex);

}



public Integer getPageCount() {

	return pageCount;

}



public Integer getPageNumb() {

	return pageNumb;

}



public Integer getPageSize() {

	return pageSize;

}



public Integer getRowCount() {

	return rowCount;

}



public Integer getPrePageNumb() {

	return prePageNumb;

}



public Integer getNextPageNumb() {

	return nextPageNumb;

}



public List getCurrentList() {

	return currentList;

}

}




**GoodsList类对其进行构造**



@WebServlet(“/goodsList”)

public class GoodsListServlet extends HttpServlet{

public static final Integer PageSize = 5;



@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

	//获取查询条件

	String goodsType = req.getParameter("goods_type");

	String goodsName = req.getParameter("goods_name");

	//从文件读出商品集合

	List<Goods> goods = GoodsFileUtil.getGoodsList();

	//查询第一个条件

	List<Goods> typeGoods = new ArrayList<Goods>();

	if (null != goodsType && !"".equals(goodsType)) {

		for (Goods goods2 : goods) {

			if (goods2.getGoodsType().contains(goodsType)) {

				typeGoods.add(goods2);

			}

		}

	} else {

		typeGoods = goods;

	}

	//查询第二个条件

	List<Goods> nameGoods = new ArrayList<Goods>();

	if (null != goodsName && !"".equals(goodsName)) {

		for (Goods goods2 : typeGoods) {

			if (goods2.getGoodsName().contains(goodsName)) {

				nameGoods.add(goods2);

			}

		}

	} else {

		nameGoods = typeGoods;

	}

	//获取当前页

	String strPageNumb = req.getParameter("pageNumb");

	Integer pageNumb = 1;

	if (null != strPageNumb && !"".equals(strPageNumb)) {

		pageNumb = Integer.valueOf(strPageNumb);

	}

	for (int i = 1; i <= goods.size(); i++) {

		Goods good = goods.get(i-1);

		good.setGoodsNum(i);

	}

	//初始化pager对象

	Pager page = new Pager(pageNumb, PageSize, nameGoods);

	req.setAttribute("page", page);

	req.setAttribute("goodsType", goodsType);

	req.setAttribute("goodsName", goodsName);

	req.getRequestDispatcher("/WEB-INF/page1.jsp")

	.forward(req, resp);

}

}




**page.jsp页面**



<%@page import=“com.wanshi.bean.Pager”%>

<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding="UTF-8"%>

<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>

分页
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值