PageView.java public class PageView<T> { /** 分页数据 **/ private List<T> records; /** 页码开始索引和结束索引 **/ private PageIndex pageindex; /** 总页数 **/ private long totalpage = 1; /** 每页显示记录数 **/ private int maxresult = 10; /** 当前页 **/ private int currentpage = 1; /** 总记录数 **/ private long totalrecord; /** 页码数量 **/ private int pagecode = 7; /** 要获取记录的开始索引 **/ public int getFirstResult() { return (this.currentpage-1)*this.maxresult; } public int getPagecode() { return pagecode; } public void setPagecode(int pagecode) { this.pagecode = pagecode; } public void setQueryResult(List<T> qr){ setRecords(qr); } public long getTotalrecord() { return totalrecord; } public void setTotalrecord(long totalrecord) { this.totalrecord = totalrecord; setTotalpage(this.totalrecord%this.maxresult==0? this.totalrecord/this.maxresult : this.totalrecord/this.maxresult+1); } public List<T> getRecords() { return records; } public void setRecords(List<T> records) { this.records = records; } public PageIndex getPageindex() { return pageindex; } public long getTotalpage() { return totalpage; } public void setTotalpage(long totalpage) { this.totalpage = totalpage; this.pageindex = PageIndex.getPageIndex(pagecode, currentpage, totalpage); } public int getMaxresult() { return maxresult; } public int getCurrentpage() { return currentpage; } public void setMaxresult(int maxresult) { this.maxresult = maxresult; } public void setCurrentpage(int currentpage) { this.currentpage = currentpage; } }
PageIndex.java public class PageIndex { private long startindex; private long endindex; public PageIndex(long startindex, long endindex) { this.startindex = startindex; this.endindex = endindex; } public long getStartindex() { return startindex; } public void setStartindex(long startindex) { this.startindex = startindex; } public long getEndindex() { return endindex; } public void setEndindex(long endindex) { this.endindex = endindex; } public static PageIndex getPageIndex(long viewpagecount, int currentPage, long totalpage){ long startpage = currentPage-(viewpagecount%2==0? viewpagecount/2-1 : viewpagecount/2); long endpage = currentPage+viewpagecount/2; if(startpage<1){ startpage = 1; if(totalpage>=viewpagecount) endpage = viewpagecount; else endpage = totalpage; } if(endpage>totalpage){ endpage = totalpage; if((endpage-viewpagecount)>0) startpage = endpage-viewpagecount+1; else startpage = 1; } return new PageIndex(startpage, endpage); } }
PageDaoSupport.java //这个为参考李刚轻量级JAVA EE那本书 public class PageDaoSupport extends HibernateDaoSupport{ public List findByPage(final String hql, final int offset, final int pageSize) { // 通过一个HibernateCallback对象来执行查询 List list = getHibernateTemplate().executeFind(new HibernateCallback() { // 实现HibernateCallback接口必须实现的方法 public Object doInHibernate(Session session) throws HibernateException, SQLException { // 执行Hibernate分页查询 List result = session.createQuery(hql).setFirstResult(offset) .setMaxResults(pageSize).list(); return result; } }); return list; } }
PagingServiceImpl.java public class PagingServiceImpl implements PagingService { private PageDaoSupport pageDaoSupport; private PageView<ProductInfo> pageView; private ProductInfoDao productInfoDao;//用来获得表中要显示的记录 public ProductInfoDao getProductInfoDao() { return productInfoDao; } public void setProductInfoDao(ProductInfoDao productInfoDao) { this.productInfoDao = productInfoDao; } public PageDaoSupport getPageDaoSupport() { return pageDaoSupport; } public void setPageDaoSupport(PageDaoSupport pageDaoSupport) { this.pageDaoSupport = pageDaoSupport; } public PageView<ProductInfo> getPageView() { return pageView; } public void setPageView(PageView<ProductInfo> pageView) { this.pageView = pageView; } /** * 首次请求时执行此方法 */ public PageView<ProductInfo> paging() { pageView.setCurrentpage(1); pageView.setTotalrecord(productInfoDao.getTotalCount()); pageView.setQueryResult(pageDaoSupport.findByPage("from ProductInfo", 0, pageView.getMaxresult())); return pageView; } /** * 每次更换页码调用此方法 */ public PageView<ProductInfo> requestPage(String requestPage) { pageView.setCurrentpage(Integer.parseInt(requestPage)); pageView.setTotalrecord(productInfoDao.getTotalCount()); pageView.setQueryResult(pageDaoSupport.findByPage("from ProductInfo", pageView.getFirstResult(), pageView.getMaxresult())); return pageView; } }
代码差不多就这些了,解析下PageView.java
每页显示记录数maxresult,显示在页面的页码数pagecode,是写死的,我们设置下总记录数即可得到总页数,设置下当前页码,可得到pageindex页码开始索引和结束索引(根据总页数,当前页码,显示页码数),好了,只剩下最后一个变量了,即records我们要查找的记录集合,我在这里是每请求一页然后查询出当页的记录。至此,所有工作已完毕,显示到页面就可以了
显示页码的JSP
<div class="pagination"> <c:if test="${pageView.getCurrentpage()!=1}"> <a href="AdminShowProductAction!requestPage.action?pageNum=${pageView.getCurrentpage()-1}" class="prev">«</a> </c:if> <c:forEach begin="${pageView.getPageindex().getStartindex()}" end="${pageView.getPageindex().getEndindex()}" var="x"> <a href="AdminShowProductAction!requestPage.action?pageNum=${x}" <c:if test="${pageView.getCurrentpage()==x}">class="current"</c:if>>${x} </a> </c:forEach> <c:if test="${pageView.getCurrentpage()!= pageView.getTotalpage()}"> <a href="AdminShowProductAction!requestPage.action?pageNum=${pageView.getCurrentpage()+ 1}" class="next">»</a> </c:if> </div>