1.封装一个PageBean类
public class PageBean<T> {
private Integer page;// 当前页数.
private Integer limit;// 每页显示记录数
private Integer totalCount;// 总记录数
private Integer totalPage;// 总页数.
private List<T> list; // 显示到浏览器的数据.
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getLimit() {
return limit;
}
public void setLimit(Integer limit) {
this.limit = limit;
}
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;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
}
2.点击按钮传递参数,页数page
3.action中接受page参数,通过调用service中的方法去查询,返回PageBean对象
4.在serviceImpl中实现分页的操作,数据封装到PageBean中
int limit = 12 ;//每页显示记录数
int totalPage = 0; //总页数
//创建PageBean
PageBean<Product> pageBean = new PageBean<Product>();
//封装页数和每页显示的记录数
pageBean.setPage(page);
pageBean.setLimit(limit);
//查询总记录数然后封装
Integer totalCount = productDao.findTotalCount(cid);
pageBean.setTotalCount(totalCount);
//总页数的封装
if(totalCount%limit==0){
totalPage = totalCount/limit ;
}else{
totalPage = totalCount/limit +1 ;
}
pageBean.setTotalPage(totalPage);
//商品数据集合和封装
int begin = (page-1)*limit;
List<Product> list = productDao.findProductByCidAndPage(cid,begin,limit);
pageBean.setList(list);
5.在dao层的操作
//查询某个分类下的总记录数
public Integer findTotalCount(Integer cid) {
String hql = "select count(*) from Product p , CategorySecond cs where p.categorySecond = cs and cs.category.cid = ?";
//String hql = "select count(*) from Product p join p.categorySecond cs join cs.category c where c.cid = ?";
List<Long> list = this.getHibernateTemplate().find(hql,cid);
//System.out.println("list:============="+list.get(0).intValue());
return list.get(0).intValue();
}
//查询商品集合
@Override
public List<Product> findProductByCidAndPage(Integer cid, Integer begin,
int limit) {
String hql = "select p from Product p , CategorySecond cs where p.categorySecond = cs and cs.category.cid = ?";
List<Product> list = this.getHibernateTemplate().executeFind(new PageHibernateCallback<Product>(hql, new Object[]{cid}, begin, limit));
return list;
}
6.在jsp中进行操作显示商品情况
<div id="result" class="result table clearfix">
<ul>
<s:iterator var="p" value="pageBean.list">
<li>
<a href="./京华亿家分页面.htm">
<img src="${pageContext.request.contextPath}/<s:property value="image"/>" width="170" height="170" style="display: inline-block;">
<span style='color:green'>
<s:property value="#p.pname"/>
</span>
<span class="price">
亿家价: ¥<s:property value="#p.market_price"/>
</span>
</a>
</li>
</s:iterator>
</ul>
</div>
7.进行分页操作查询商品
<div class="pagination">
第 <s:property value="pageBean.page"/>/<s:property value="pageBean.totalPage"/>页
<s:if test="pageBean.page != 1">
<a href="${ pageContext.request.contextPath }/product/product_findByCid.action?cid=<s:property value="cid"/>&page=1" class="firstPage"> </a>
<a href="${ pageContext.request.contextPath }/product/product_findByCid.action?cid=<s:property value="cid"/>&page=<s:property value="pageBean.page-1"/>" class="previousPage"> </a>
</s:if>
<s:iterator var="i" begin="1" end="pageBean.totalPage" step="1">
<s:if test="pageBean.page==#i">
<span class="currentPage"><s:property value="#i"/></span>
</s:if>
<s:else>
<a href="${ pageContext.request.contextPath }/product/product_findByCid.action?cid=<s:property value="cid"/>&page=<s:property value="#i"/>"><s:property value="#i"/></a>
</s:else>
</s:iterator>
<s:if test="pageBean.page != pageBean.totalPage">
<a class="nextPage" href="${ pageContext.request.contextPath }/product/product_findByCid.action?cid=<s:property value="cid"/>&page=<s:property value="pageBean.page+1"/>"> </a>
<a class="lastPage" href="${ pageContext.request.contextPath }/product/product_findByCid.action?cid=<s:property value="cid"/>&page=<s:property value="pageBean.totalPage"/>"> </a>
</s:if>
</div>