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>