商品增删改查之分页显示
首先需要创建一个javaBean
public class PageBean<T> {
private Integer currentPage;//当前页数
private Integer pagesize;//每页显示的数目
private Integer totalCount;//总条目数
private Integer totalPage;//总页数
private List<T> list;//用来封装商品集合
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getPagesize() {
return pagesize;
}
public void setPagesize(Integer pagesize) {
this.pagesize = pagesize;
}
public Integer getTotalCount() {
return totalCount;
}
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
}
然后需要请求servlet用来处理
public String findByCid(HttpServletRequest request,
HttpServletResponse response) {
try {
// 接受参数
String cid = request.getParameter("cid");
Integer currentPage = Integer.parseInt(request
.getParameter("currentPage"));
// 调用业务层
ProductService productService = (ProductService) BeanFactory.getBean("productService");
PageBean<Product> pageBean = productService.findByPageCid(cid,
currentPage);
// 将数据放入域中
request.setAttribute("pageBean", pageBean);
return "/jsp/product_list.jsp";
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
}
然后调用service来处理封装数据
/*
* 用于分页查询商品分类
* 并且在业务层封装pageBean的信息
*/
public PageBean<Product> findByPageCid(String cid, Integer currentPage) throws SQLException {
PageBean<Product> pageBean = new PageBean<Product>();
//设置当前页数
pageBean.setCurrentPage(currentPage);
//设置每页读取的商品数目
Integer pagesize = 12;
pageBean.setPagesize(pagesize);
//设置总的记录数
ProductDao productDao = (ProductDao) BeanFactory.getBean("productDao");
Integer count = productDao.findCount(cid);
pageBean.setTotalCount(count);
//设置总页数
double tc = count;
Double totalPage = Math.ceil(tc/pagesize);
pageBean.setTotalPage(totalPage.intValue());
//设置每页显示数据的商品的集合 1 10 2 11-20
int begin = (currentPage-1)*pagesize;
List<Product> list = productDao.findByCid(cid, begin, pagesize);
pageBean.setList(list);
return pageBean;
}
调用DAO来查询数据库
/*
* 分页查询商品
*/
public List<Product> findByCid(String cid, int begin, Integer pagesize) throws SQLException {
QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
String sql = "select * from product where pflag = ? and cid = ? order by pdate desc limit ?,?";
List<Product> list = queryRunner.query(sql, new BeanListHandler<Product>(Product.class),0,cid,begin,pagesize);
return list;
}
将封装的数据返回并在jsp中获取
<div style="width: 380px; margin: 0 auto; margin-top: 50px;">
<ul class="pagination" style="text-align: center; margin-top: 10px;">
<c:if test="${pageBean.currentPage!=1 }">
<li ><a
href="${pageContext.request.contextPath}/ProductServlet?cid=${param.cid }&method=findByCid¤tPage=${pageBean.currentPage-1 }"
aria-label="Previous"><span aria-hidden="true">«</span></a></li>
</c:if>
<c:forEach var="i" begin="1" end="${ pageBean.totalPage }">
<c:if test="${pageBean.currentPage==i}">
<li class="active"><a href="#">${i }</a></li>
</c:if>
<c:if test="${pageBean.currentPage!=i}">
<li ><a
href="${pageContext.request.contextPath}/ProductServlet?cid=${param.cid }&method=findByCid¤tPage=${i }">${i}</a></li>
</c:if>
</c:forEach>
<c:if test="${ pageBean.currentPage != pageBean.totalPage }">
<li ><a
href="${ pageContext.request.contextPath }/ProductServlet?cid=${param.cid }&method=findByCid¤tPage=${pageBean.currentPage+1 }"
aria-label="Next"> <span aria-hidden="true">»</span>
</a></li>
</c:if>
</ul>
</div>
<!-- 分页结束======================= -->