public interface Paginable { public int getTotalCount(); public int getPageCount(); public int getPageSize(); public int getCurrentPage(); public int getStartIndex(); public boolean isFirstPage(); public boolean isLastPage(); public int getNextPage(); public int getPrePage(); }
public class SimplePage implements Paginable { public final static int PAGESIZE = 4; // Total count of records protected int totalCount; // The size of records per page protected int pageSize = PAGESIZE; // Current page protected int currentPage; // The count of pages private int pageCount; public SimplePage() { } public SimplePage(int totalCount) { setPageSize(PAGESIZE); setTotalCount(totalCount); setCurrentPage(1); } public SimplePage(int currentPage, int totalCount) { setPageSize(PAGESIZE); setTotalCount(totalCount); setCurrentPage(currentPage); } public SimplePage(int currentPage, int pageSize, int totalCount) { setPageSize(pageSize); setTotalCount(totalCount); setCurrentPage(currentPage); } public boolean isFirstPage() { return currentPage <= 1; } public boolean isLastPage() { return currentPage >= pageCount; } public int getNextPage() { if (isLastPage()) { return currentPage; } else { return currentPage + 1; } } public int getPrePage() { if (isFirstPage()) { return currentPage; } else { return currentPage - 1; } } public int getCurrentPage() { return currentPage; } public int getStartIndex() { return (currentPage - 1) * pageSize; } public void setCurrentPage(int currentPage) { if (totalCount <= 0) this.currentPage = 0; else if (currentPage >= pageCount) this.currentPage = pageCount; else if (currentPage <= 1) this.currentPage = 1; else { this.currentPage = currentPage; } } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { if (totalCount > 0) { this.totalCount = totalCount; pageCount = totalCount / pageSize; if (totalCount % pageSize > 0) pageCount++; } else { this.totalCount = 0; } } public int getPageCount() { return pageCount; } public void setPageCount(int pageCount) { this.pageCount = pageCount; } }
import java.util.List; @SuppressWarnings("serial") public class Pagination extends SimplePage implements java.io.Serializable, Paginable { public Pagination() { } @SuppressWarnings("unchecked") public Pagination(int totalCount, List list) { super(totalCount); this.list = list; } @SuppressWarnings("unchecked") public Pagination(int currentPage, int totalCount, List list) { super(currentPage, totalCount); this.list = list; } @SuppressWarnings("unchecked") public Pagination(int currentPage, int pageSize, int totalCount, List list) { super(currentPage, pageSize, totalCount); this.list = list; } public Pagination(int currentPage, int pageSize, int totalCount) { super(currentPage, pageSize, totalCount); } public int getFirstResult() { return (currentPage - 1) * pageSize; } @SuppressWarnings("unchecked") private List list; @SuppressWarnings("unchecked") public List getList() { return list; } @SuppressWarnings("unchecked") public void setList(List list) { this.list = list; } }
public class PageUtil { public static String getPageBar1(Paginable page, String url) { String temp = ""; if (url.indexOf("?") == -1) { temp = "?"; } else { temp = "&"; } StringBuffer pageBar = new StringBuffer(); if (page.isFirstPage()) pageBar.append("First Previous "); else { pageBar.append("<a href='").append(url).append(temp).append( "currentPage=1'>First</a> "); pageBar.append("<a href='").append(url).append(temp).append( "currentPage=").append(page.getPrePage()).append( "'>Previous</a> "); } if (page.isLastPage()) pageBar.append("Next Last "); else { pageBar.append("<a href='").append(url).append(temp).append( "currentPage=").append(page.getNextPage()).append( "'>Next</a> "); pageBar.append("<a href='").append(url).append(temp).append( "currentPage=").append(page.getPageCount()).append( "'>Last</a> "); } pageBar.append(" Total Page:").append(page.getPageCount()).append( " "); pageBar.append(" Go<select name='page' onChange=\"location='"); pageBar.append(url).append(temp).append( "currentPage='+this.options[this.selectedIndex].value\">"); int begin = (page.getCurrentPage() > 10) ? page.getCurrentPage() - 10 : 1; int end = (page.getPageCount() - page.getCurrentPage() > 10) ? page .getCurrentPage() + 10 : page.getPageCount(); for (int i = begin; i <= end; i++) { if (i == page.getCurrentPage()) pageBar.append("<option value='").append(i).append( "' selected>-").append(i).append("-</option>"); else pageBar.append("<option value='").append(i).append("'>-") .append(i).append("-</option>"); } pageBar.append("</select>"); return pageBar.toString(); } //Implement other page bar you wanted public static String getPageBar2(Paginable page, String url){ return ""; } }
import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.criterion.DetachedCriteria; import org.hibernate.criterion.Projections; import org.springframework.orm.hibernate3.HibernateCallback; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import page.Pagination; public abstract class AbstractDaoManager extends HibernateDaoSupport { private boolean cacheQueries = false; private String queryCacheRegion; public void setCacheQueries(boolean cacheQueries) { this.cacheQueries = cacheQueries; } public void setQueryCacheRegion(String queryCacheRegion) { this.queryCacheRegion = queryCacheRegion; } public void save(final Object entity) { getHibernateTemplate().save(entity); } public void persist(final Object entity) { getHibernateTemplate().save(entity); } public void update(final Object entity) { getHibernateTemplate().update(entity); } public void delete(final Object entity) { getHibernateTemplate().delete(entity); } public Object load(final Class entity, final Serializable id) { return getHibernateTemplate().load(entity, id); } public Object get(final Class entity, final Serializable id) { return getHibernateTemplate().get(entity, id); } public List findAll(final Class entity) { return getHibernateTemplate().find("from " + entity.getName()); } public List findByNamedQuery(final String namedQuery) { return getHibernateTemplate().findByNamedQuery(namedQuery); } public List findByNamedQuery(final String query, final Object parameter) { return getHibernateTemplate().findByNamedQuery(query, parameter); } public List findByNamedQuery(final String query, final Object[] parameters) { return getHibernateTemplate().findByNamedQuery(query, parameters); } public List find(final String query) { return getHibernateTemplate().find(query); } public List find(final String query, final Object parameter) { return getHibernateTemplate().find(query, parameter); } public Pagination findPageByCriteria( final DetachedCriteria detachedCriteria) { return findPageByCriteria(detachedCriteria, Pagination.PAGESIZE, 0); } public Pagination findPageByCriteria( final DetachedCriteria detachedCriteria, final int startIndex) { return findPageByCriteria(detachedCriteria, Pagination.PAGESIZE, startIndex); } public Pagination findPageByCriteria( final DetachedCriteria detachedCriteria, final int pageSize, final int currentPage) { return (Pagination) getHibernateTemplate().execute( new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { Criteria criteria = detachedCriteria .getExecutableCriteria(session); int totalCount = ((Integer) criteria.setProjection( Projections.rowCount()).uniqueResult()) .intValue(); criteria.setProjection(null); Pagination page = new Pagination(currentPage, pageSize, totalCount); List items = criteria.setFirstResult(page.getStartIndex()) .setMaxResults(pageSize).list(); page.setItems(items); return page; } }); } public List findAllByCriteria(final DetachedCriteria detachedCriteria) { return (List) getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { Criteria criteria = detachedCriteria .getExecutableCriteria(session); return criteria.list(); } }); } public int getCountByCriteria(final DetachedCriteria detachedCriteria) { Integer count = (Integer) getHibernateTemplate().execute( new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { Criteria criteria = detachedCriteria .getExecutableCriteria(session); return criteria.setProjection(Projections.rowCount()) .uniqueResult(); } }); return count.intValue(); } }