商场分页----涉及分页知识点

本文介绍了一种在商城网站中实现商品按类别分页显示的方法。通过在Service层封装PageBean对象,包括商品信息、当前页、总页数等信息,实现了商品的分页查询。同时,详细展示了如何在DAO层进行数据查询,以及如何在前端实现分页导航。

描述:当点击一个商城类别时将这一类别所有的商品显示出来,并设置分页

要显示一个商品的分页 首先我们要封装一个页的pageBean(由图 很显然我们要包含商品信息List<Product> 当前页currentPage  总页数TotalPage  当前页所含商品总个数currentCount  商品总个数totalCount)

要想获得分页我们需要在service层将PageBean的信息封装起来返回到web层

具体的步骤:

public PageBean findProductBycid(String cid, int currentPage, int currentCount) {
		ProductDao dao = new ProductDao();
		// 封装一个pagebean返回到web层
		PageBean<Product> pageBean = new PageBean<Product>();
		// 1.封装当前页

		pageBean.setCurrentPage(currentPage);

		// 2.每页显示的条数

		pageBean.setCurrentCount(currentCount);

		// 3.封装总条数
		int totalCount = 0;
		try {
			totalCount = dao.getTotal(cid);
		} catch (SQLException e) {

			e.printStackTrace();
		}
		pageBean.setTotalCount(totalCount);
		// 4.封装总页数
		// ceil函数向上取整
		int totalPage = (int) Math.ceil(1.0 * totalCount / currentCount);
		pageBean.setTotalPage(totalPage);

		// 5.当前页显示数据
		List<Product> list = null;
		// 当前页与起始索引的关系
		int index = (currentPage - 1) * currentCount;
		try {
			list = dao.findProductBypage(cid, index, currentCount);
		} catch (SQLException e) {

			e.printStackTrace();
		}
		pageBean.setList(list);
		return pageBean;
	}

dao层代码段:


	public int getTotal(String cid) throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select count(*) from product where cid=?";
		Long query = (Long) runner.query(sql, new ScalarHandler(), cid);
		return query.intValue();
	}

	public List<Product> findProductBypage(String cid, int index, int currentCount) throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select * from product where cid=? limit ?,?";
		return runner.query(sql, new BeanListHandler<Product>(Product.class), cid, index, currentCount);
	}

 关于判断分页的页面

<!--分页 -->
	<div style="width: 380px; margin: 0 auto; margin-top: 50px;">
		<ul class="pagination" style="text-align: center; margin-top: 10px;">
			<!-- 上一页 -->
			<c:choose >
			  <c:when test="${pageBean.currentPage==1 } ">
			     <li class="disabled">
			        <a href="javascript:void(0);" aria-label="Previous">
						<span aria-hidden="true">&laquo;</span>
					</a>
			     </li>
			  </c:when>
			  <c:when test="${pageBean.currentPage!=1 }">
			      <li>
					<a href="${pageContext.request.contextPath}/Product?method=productListByCid&cid=${cid}&currentPage=${pageBean.currentPage-1 }" aria-label="Previous">
						<span aria-hidden="true">&laquo;</span>
					</a>
				</li>
			  </c:when>
			</c:choose>
			
			<!-- 1.显示每一页 -->
			<c:forEach begin="1" end="${pageBean.totalPage }" var="page">
			    <!-- 判断是否是当前页  当点击的是当前页 则不跳转 如果点击的不是当前页 则跳转 并且要不把点击的当前页数据传递过去 获取数据-->
			    <c:choose>
			       <c:when test="${page==pageBean.currentPage }">
			          <li class="active"><a href="javascript:void(0);">${page }</a></li>
			       </c:when>
			       <c:when test="${page!=pageBean.currentPage }">
			          <!-- 如果不为当前页  -->
			          <li><a href="${pageContext.request.contextPath}/Product?method=productListByCid&cid=${cid }&currentPage=${page }">${page }</a></li>
			       </c:when>
			    </c:choose>
			</c:forEach>
			
			<!--下一页  -->
		<c:choose >
			  <c:when test="${pageBean.currentPage==pageBean.totalPage } ">
			     <li class="disabled">
			        <a href="javascript:void(0);" aria-label="Next">
						<span aria-hidden="true">&raquo;</span>
					</a>
			     </li>
			  </c:when>
			  <c:when test="${pageBean.currentPage!=pageBean.totalPage }">
			      <li>
					<a href="${pageContext.request.contextPath}/Product?method=productListByCid&cid=${cid}&currentPage=${pageBean.currentPage+1 }" aria-label="Next">
						<span aria-hidden="true">&raquo;</span>
					</a>
				</li>
			  </c:when>
			</c:choose>
		</ul>
	</div>
	<!-- 分页结束 -->

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值