來自:http://wangyu./blog/211265
一個(gè)很好用的分頁
先是一個(gè)page的bean:
package com.leatherstore.other;
public class Page {
// 是否有上一頁
private boolean hasPrePage;
//是否有下一頁
private boolean hasNextPage;
// 每頁的數(shù)量
private int everyPage;
// 總頁數(shù)
private int totalPage;
//當(dāng)前頁
private int currentPage;
// 起始點(diǎn)
private int beginIndex;
// 總記錄數(shù)
private int totalCount;
//@return totalCount
public int getTotalCount() {
return totalCount;
}
//@param totalCount 要設(shè)置的 totalCount
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
//The default constructor
public Page(){
}
// construct the page by everyPage
public Page(int everyPage){
this.everyPage = everyPage;
}
//The whole constructor
public Page(boolean hasPrePage, boolean hasNextPage,
int everyPage, int totalPage,
int currentPage, int beginIndex,int totalCount) {
this.hasPrePage = hasPrePage;
this.hasNextPage = hasNextPage;
this.everyPage = everyPage;
this.totalPage = totalPage;
this.currentPage = currentPage;
this.beginIndex = beginIndex;
this.totalCount = totalCount;
}
public int getBeginIndex() {
return beginIndex;
}
public void setBeginIndex(int beginIndex) {
this.beginIndex = beginIndex;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getEveryPage() {
return everyPage;
}
public void setEveryPage(int everyPage) {
this.everyPage = everyPage;
}
public boolean getHasNextPage() {
return hasNextPage;
}
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
public boolean getHasPrePage() {
return hasPrePage;
}
public void setHasPrePage(boolean hasPrePage) {
this.hasPrePage = hasPrePage;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
}
然后構(gòu)建一個(gè)page的工廠PageUtil:
package com.leatherstore.other;
public class PageUtil {
// Use the origin page to create a new page
public static Page createPage(Page page, int totalRecords) {
return createPage(page.getEveryPage(), page.getCurrentPage(),
totalRecords);
}
//the basic page utils not including exception handler
public static Page createPage(int everyPage, int currentPage,
int totalRecords) {
everyPage = getEveryPage(everyPage);
currentPage = getCurrentPage(currentPage);
int beginIndex = getBeginIndex(everyPage, currentPage);
int totalPage = getTotalPage(everyPage, totalRecords);
boolean hasNextPage = hasNextPage(currentPage, totalPage);
boolean hasPrePage = hasPrePage(currentPage);
return new Page(hasPrePage, hasNextPage, everyPage, totalPage,
currentPage, beginIndex, totalRecords);
}
private static int getEveryPage(int everyPage) {
return everyPage == 0 ? 10 : everyPage;
}
private static int getCurrentPage(int currentPage) {
return currentPage == 0 ? 1 : currentPage;
}
private static int getBeginIndex(int everyPage, int currentPage) {
return (currentPage - 1) * everyPage;
}
private static int getTotalPage(int everyPage, int totalRecords) {
int totalPage = 0;
if (totalRecords % everyPage == 0)
totalPage = totalRecords / everyPage;
else
totalPage = totalRecords / everyPage + 1;
return totalPage;
}
private static boolean hasPrePage(int currentPage) {
return currentPage == 1 ? false : true;
}
private static boolean hasNextPage(int currentPage, int totalPage) {
return currentPage == totalPage || totalPage == 0 ? false : true;
}
}
然后建一個(gè)專用的bean:
package com.leatherstore.hibernate.domain;
import java.util.List;
import com.leatherstore.other.Page;
public class Result {
private Page page; //分頁信息
private List content; //每頁顯示的集合
//The default constructor
public Result() {
super();
}
//The constructor using fields
public Result(Page page, List content) {
this.page = page;
this.content = content;
}
public List getContent() {
return content;
}
public Page getPage() {
return page;
}
public void setContent(List content) {
this.content = content;
}
public void setPage(Page page) {
this.page = page;
}
}
然后在數(shù)據(jù)訪問層寫兩個(gè)方法:
ProductDAO:
public List getProductByPage(Page page);
public int getProductCount(); //返回?cái)?shù)據(jù)的總數(shù)
ProductDAO的接口實(shí)現(xiàn)ProductDAOImpl:
//為了在spring里統(tǒng)一編程風(fēng)格:我用的回調(diào)的方法
public List getProductByPage(final Page page) {
return this.getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query=session.createQuery("from Productinfo");
query.setFirstResult(page.getBeginIndex()); //hibernate分頁的精髓 呵呵
query.setMaxResults(page.getEveryPage());
return query.list();
}
});
}
public int getProductCount() {
List list=this.getHibernateTemplate().find("select count(*) from Productinfo");
return ((Integer)list.iterator().next()).intValue();
}
然后是業(yè)務(wù)層:
IProduct:
public Result listProduct(Page page);
然后IProduct接口的實(shí)現(xiàn):IProductImpl:
private ProductDAO productDAO;
public void setProductDAO(ProductDAO productDAO){
this.productDAO=productDAO;
}
public Result listProduct(Page page) {
int totalRecords = this.productDAO.getProductCount();
page = PageUtil.createPage(page, totalRecords);
List products = this.productDAO.getProductByPage(page);
return new Result(page, products);
}
然后再代理層:
ProductProxy:
IProduct pro=(IProduct)AppContext.getInstance().getappcontext().getBean("productServicewithTran");
public Result productlist(Page page){
try{
return pro.listProduct(page);
}catch(DataAccessException ex){
ex.printStackTrace();
return null;
}
}
呵呵 終于到productAction啦
顯示前方法的代碼
Page page = new Page(); //實(shí)例化一個(gè)page對象
page.setEveryPage(10); //設(shè)置每頁顯示的條數(shù)
page.setCurrentPage(1); //為第一頁
Result result = pdp.productlist(page);
request.setAttribute("page", pageinfo);
request.setAttribute("productlist", list);
return mapping.findForward("showProduct");
接著就是jsp頁面了
<logic:iterate id="product" name="productlist">
//中間迭代所要顯示的數(shù)據(jù)
</logic:iterate>
<tr>
<td width="80" height="30"> </td>
<logic:equal value="true" name="page" property="hasPrePage">
<td width="150" height="30"><div align="right"><a href="../product.do?method=showProductByTag&index=first&msg=${msg }">首頁</a></div></td>
<td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=prew&pageno=${page.currentPage -1}&msg=${msg }">上一頁</a></div></td>
</logic:equal>
<logic:notEqual value="true" name="page" property="hasPrePage">
<td width="150" height="30"><div align="right">首頁</div></td>
<td width="80" height="30"><div align="center">上一頁</div></td>
</logic:notEqual>
<logic:equal value="true" name="page" property="hasNextPage">
<td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=next&pageno=${page.currentPage +1 }&msg=${msg }">下一頁</a></div></td>
<td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=end&pageno=${page.totalPage }&msg=${msg }">尾頁</a></div></td>
</logic:equal>
<logic:notEqual value="true" name="page" property="hasNextPage">
<td width="80" height="30"><div align="center">下一頁</div></td>
<td width="80" height="30"><div align="center">尾頁</div></td>
</logic:notEqual>
<td height="30" colspan="3"><div align="center">頁次${page.currentPage }/${page.totalPage } 共${page.totalCount }條記錄</div> <div align="center"></div></td>
</tr>
可以顯示相應(yīng)的頁面信息
然后productAction里面的showProductByTag代碼如下:
Page page = new Page();
page.setEveryPage(10);
String pagemark = request.getParameter("goto");
if (pagemark == null) {
String state = request.getParameter("index");
String pageno = request.getParameter("pageno");
System.out.println("pageno=" + pageno);
if ("first".equals(state)) {
page.setCurrentPage(1);
Result result = pdp.productlist(page);
request.setAttribute("page", result.getPage());
request.setAttribute("productlist", result.getContent());
} else if ("prew".equals(state)) {
page.setCurrentPage(Integer.parseInt(pageno));
Result result = pdp.productlist(page);
request.setAttribute("page", result.getPage());
request.setAttribute("productlist", result.getContent());
} else if ("next".equals(state)) {
page.setCurrentPage(Integer.parseInt(pageno));
Result result = pdp.productlist(page);
request.setAttribute("page", result.getPage());
request.setAttribute("productlist", result.getContent());
} else if ("end".equals(state)) {
page.setCurrentPage(Integer.parseInt(pageno));
Result result = pdp.productlist(page);
request.setAttribute("page", result.getPage());
request.setAttribute("productlist", result.getContent());
}
} else {
page.setCurrentPage(Integer.parseInt(pagemark));
Result result = pdp.productlist(page);
request.setAttribute("page", result.getPage());
request.setAttribute("productlist", result.getContent());
}
return mapping.findForward("showProduct");