1.建分頁示圖類: private int totalNum; //分頁查詢的List結(jié)果 private List pageList; //每頁顯示多少條數(shù)據(jù) private int pageSize; //總共多少頁 private int pageTotal; //當前是第幾頁 private int page; 屬性get 和set方法 2.分頁接口及實現(xiàn)返回符合條件數(shù)據(jù)集合: public class FindByHqlDaoImpl extends HibernateDaoSupport implements FindByHqlDao public List findByHql(final String hql,final int offset, final int pageSize) { List list = getHibernateTemplate() .executeFind(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Query query=session.createQuery(hql); if(offset>0){ query=query.setFirstResult(offset); } if(pageSize>0){ query=query.setMaxResults(pageSize); } return query.list(); } }); return list; } 重寫這個方法findByHql,添加含查詢條件的參數(shù); public List findByHql(final String hql , final Object value ,final int offset) { return this.findByHql(hql, value, offset, 0); } 3.action中接收頁面的參數(shù) 參數(shù)1:從第幾條數(shù)據(jù)開始查詢 參數(shù)2:每次查詢幾條數(shù)據(jù) public String findArticlesByFieldIdMethod() { //pageNum 一次查詢多少條數(shù)據(jù) int pageNum = 0; //pager.offset 重第幾條開始查詢 String pageNum_str = ServletActionContext.getRequest().getParameter( "pager.offset"); //model類中取出分頁示圖 pageView = model.getArticlePageview(pageNum, temp, 10); 4.MOdle類中整理頁面查詢條件:封裝到Object類型的數(shù)組 public PageView findNewProbationTenant(int pageNum,Object values, int pageSize) { int totalNum=0; int page = pageNum/pageSize+1; //totalNum一共多少條數(shù)據(jù) pageView.setTotalNum(totalNum); //當前是第幾頁 pageView.setPae(page); //總共多少頁 pageView.setPageTotal(pageTotal); //每頁顯示多少條數(shù)據(jù) pageView.setPageSize(pageSize); List pageFields = fieldDao.pageFieldsinfo(pageNum,values, pageSize); return pageView; 5.fieldDao及實現(xiàn)extends FindByHqlDaoImpl implements ArticleDao
public List<Article> getPageviewListForArt(int pageNum, Object[] values, int pageSize) { hql="from Article art where art.artArray.articleId=? and art.fieldId.fieldId=? order by art.artStatus desc, art.artSequence"; list = findByHql(hql, values, pageNum, pageSize); return list; } 6.list 經(jīng)返回到j(luò)sp: <pg:pager items="${pageView.totalNum}" export="currentPageNo = pageNumber" url="article/findArticlesByFieldId.action" maxPageItems="${pageView.pageSize}" maxIndexPages="6"> <pg:param name="pageView.pageSize"/> <pg:param name="fieldId" value="${fieldId}"/> <pg:param name="superArtId" value="0"/> <pg:first> <a href="${ pageUrl }">首頁</a> </pg:first> <pg:prev> <a href="${ pageUrl }">前頁</a> <pg:pages> <c:choose> <c:when test="${ currentPageNo eq pageNumber }"> <font color="red">${ pageNumber }</font> </c:when> <c:otherwise> </c:otherwise>
|