**分页功能实现**
> 分页功能实现思路:
> 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” %>