描述:当点击一个商城类别时将这一类别所有的商品显示出来,并设置分页
要显示一个商品的分页 首先我们要封装一个页的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">«</span>
</a>
</li>
</c:when>
<c:when test="${pageBean.currentPage!=1 }">
<li>
<a href="${pageContext.request.contextPath}/Product?method=productListByCid&cid=${cid}¤tPage=${pageBean.currentPage-1 }" aria-label="Previous">
<span aria-hidden="true">«</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 }¤tPage=${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">»</span>
</a>
</li>
</c:when>
<c:when test="${pageBean.currentPage!=pageBean.totalPage }">
<li>
<a href="${pageContext.request.contextPath}/Product?method=productListByCid&cid=${cid}¤tPage=${pageBean.currentPage+1 }" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</c:when>
</c:choose>
</ul>
</div>
<!-- 分页结束 -->