按类型分页功能的实现

实现思想

分页功能
因为是分类查询所以和一般的分页功能的sql语句不一样,需要在查找的时候添加一个判断条件where cid=?
因为需要操作的数据多,类型繁杂,所以应该创建一个对象将其封装起来,并在对象内对其属性进行操作,这样尽量做到在后端计算数据,在前端取数据。
一般分页会有:首页,上一页,具体那一页,下一页,尾页,等功能。
当点击首页时需要传入,page=1,cid=1
当点击尾页时需要传入,page=totalpage,cid=1
当点击上一页时需要传入,page=page-1(需要判断,有没有上一页),cid=1
当点击下一页时需要传入,page=page+1(需要判断,有没有下一页),cid=1
当点击具体分页的时候需要传入,page=当前页的值,cid=1;(这个需要用到for循环后端传来的totalpage,然后取到值就是前页的值)
所以经过分析pagemodel需要的参数有:indexpage每页开始的索引,pagesize每页显示的商品数量固定的,cid分类信息,preProductList每页查询到的集合,sumpage共有多少页,uppage上一页,headerpage首页,nextpage下一页,endpage尾页。nowpage当前页数。

nowpage,当前页数是前端传过来的数据
indexpage,每页开始的索引是(当前页数-1)X每页的商品数
pagesize,每页的商品数是个固定值,这次是12
cid,商品分类信息,是前端传过来的数据,
preProductList,每页显示的商品,是一个list集合是通过sql语句查出来的
totalpage,共有多少页,是通过sql语句查出来的
uppage,上一页,是用当前页-1(需要判断是否有上一页)
headerpage,首页就是page=1
nextpage,下一页,是用当前页+1(需要判断是否有下一页)
endpage,尾页就是page=toalpage

pojo代码


import java.util.List;

public class PageModel {
	private int nowpage;	//当前页
	private int pagesize=12;	//每页总数
	private int totalpage;	//共多少页
	private int uppage;	//上一页
	private int nextpage;	//下一页
	private String cid;  //分类id
	private int beganindex;		//每页起始页数
	private List<Product> preProductList;   //分类产品总数


	@Override
	public String toString() {
		return "PageModel [nowpage=" + nowpage + ", pagesize=" + pagesize + ", totalpage=" + totalpage + ", uppage="
				+ uppage + ", nextpage=" + nextpage + ", cid=" + cid + ", beganindex=" + beganindex
				+ ", preProductList=" + preProductList + "]";
	}

	public PageModel() {
		super();
		// TODO Auto-generated constructor stub
	}

	public PageModel(int nowpage,  int sumlist, String cid) {
		super();
		this.nowpage = nowpage;
		this.cid = cid;
		this.totalpage = (pagesize>sumlist)?1:((sumlist%pagesize==0)?(sumlist/pagesize):(sumlist/pagesize+1));
		this.uppage = (this.nowpage==1)?(1):(this.nowpage-1);
		this.nextpage = (this.nowpage==totalpage)?(totalpage):(this.nowpage+1);
		this.beganindex = (this.nowpage-1)*pagesize;
	}

	public int getNowpage() {
		return nowpage;
	}

	public void setNowpage(int nowpage) {
		this.nowpage = nowpage;
	}

	public int getPagesize() {
		return pagesize;
	}
	public int getTotalpage() {
		return totalpage;
	}

	public void setTotalpage(int sumlist) {
		this.totalpage = (sumlist%pagesize==0)?(sumlist/pagesize):(sumlist/pagesize+1);
	}

	public int getUppage() {
		return uppage;
	}

	public void setUppage() {
		this.uppage = (nowpage==1)?(1):(nowpage-1);
	}

	public int getNextpage() {
		return nextpage;
	}

	public void setNextpage() {
		this.nextpage = (nowpage==totalpage)?(totalpage):(nowpage+1);
	}

	public String getCid() {
		return cid;
	}

	public void setCid(String cid) {
		this.cid = cid;
	}
	
	public List<Product> getPreProductList() {
		return preProductList;
	}

	public void setPreProductList(List<Product> preProductList) {
		this.preProductList = preProductList;
	}

	public int getBeganindex() {
		return beganindex;
	}

	public void setBeganindex() {
		this.beganindex = (nowpage-1)*pagesize;
	}	
}

dao代码

``
public int findByPage(String cid) throws Exception {
QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
List totalproduct = qr.query(“select * from product where cid=?”, new BeanListHandler(Product.class) ,cid);
return totalproduct.size();
}

public List<Product> findByPage(String cid, int page) throws Exception {
	QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
	System.out.println(page+"=========");
	List<Product> list = qr.query("select * from product where cid=? limit ? ,12", new BeanListHandler<Product>(Product.class),cid,page);
	return list;
}```

service代码

		PageModel pm=new PageModel();
		pm = pdao.findByPage(page,cid);
		return pm;
	
	}```
	
## servlet代码
public String findByPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
	String page =request.getParameter("page");
	String cid=request.getParameter("cid");
	ProductService ps=new ProductServiceImp();
	PageModel pm=new PageModel();
	pm = ps.findBypage(page,cid);
	request.setAttribute("pageModel",pm);
	System.out.println("接收到的两个参数是page==="+page+"cid======"+cid);
	System.out.println(pm);
	return "jsp/product_list.jsp";
}```

jsp代码

		<c:if test="${empty pageModel.preProductList }"><h2>SORRY NOT PRODUCT</h2></c:if>
		<c:if test="${not empty pageModel.preProductList }">
		<div style="width:380px;margin:0 auto;margin-top:50px;">
			<ul class="pagination" style="text-align:center; margin-top:10px;">
				<li class="disabled"><a href="ProductServlet?metod=findByPage&page=1&cid=${pageModel.cid }" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>
				
				<c:forEach  begin="1" end="${pageModel.totalpage }"  var="i">
				<li class="active"><a href="ProductServlet?metod=findByPage&page=${i }&cid=${pageModel.cid }">${i }</a></li>
				</c:forEach>
				
				<li class="disabled"><a href="ProductServlet?metod=findByPage&page=${pageModel.totalpage }&cid=${pageModel.cid }" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>
			</ul>
		</div>
		</c:if>```	
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值