在JavaWeb項目中,分頁是一個非常常見且重要的一個小方面。本次作為記載和學習,記錄項目中出現的分頁並做好學習記錄。在這里,用的是SSH框架。框架可以理解如下圖:
在JSP頁面,描寫的代碼如下:
1 <div align="center"> 2 <c:if test="${page.currentPage>1}"> 3 <a href="show_findStessayAll.action?currentPage=1" >首頁</a> 4 <a href="show_findStessayAll.action?currentPage=${page.currentPage-1 }">上一頁</a> 5 </c:if> 6 <c:if test="${page.currentPage != page.totalPage }"> 7 <a href="show_findStessayAll.action?currentPage=${page.currentPage+1 }">下一頁</a> 8 <a href="show_findStessayAll.action?currentPage=${page.totalPage}">末頁</a> 9 </c:if> 10 11 <form action="show_findStessayAll.action"> 12 共${page.totalPage}頁 13 <input type="text" value="${page.currentPage}" name="currentPage" size="1">頁 14 <input type="submit" value="go"> 15 </form> 16</div>
Action部分代碼:
1 public String findAcadcommAll(){ 2 //page存儲頁面數據 3 Page<Acadcomm> page = new Page<Acadcomm>(); 4 5 //總記錄數 6 int totalRecord = showService.findAcadcommRecord(); 7 if(totalRecord!=0){ 8 page.setTotalRecord(totalRecord); 9 //總頁數 10 int totalPage = ( totalRecord % page.getPageSize() == 0) ? totalRecord / page.getPageSize():totalRecord / page.getPageSize()+1; 11 page.setTotalPage(totalPage); 12 //當前頁 13 int currentPage = 1; 14 String currentPageString = req.getParameter("currentPage"); 15 System.out.println("currentPageString:"+currentPageString); 16 if(currentPageString != null){ 17 currentPage = Integer.parseInt(currentPageString); 18 } 19 page.setCurrentPage(currentPage); 20 System.out.println("currentPage:"+currentPage); 21 22 String hql = "from Acadcomm a"; // 查詢語句 23 //要顯示的數據 24 if(totalRecord % page.getPageSize()!=0 && currentPage==totalPage ){ 25 page.setDataList(showService.queryForPage(acadcomm,hql, (currentPage-1)*page.getPageSize(), totalRecord % page.getPageSize())); 26 27 }else { 28 page.setDataList(showService.queryForPage(acadcomm,hql, (currentPage-1)*page.getPageSize(),page.getPageSize())); 29 } 30 } 31 req.setAttribute("page", page); 32 33 return "findAcadcommAllSuccess"; 34 }
調用Service
public int findStessayRecord(String hql);//返回總記錄數 public List<Stessay> queryForPage(Stessay stessay, String hql, int offset, int length);//返回當前頁面數據
ServiceImpl調用對應Dao里面的函數
1 @Override 2 public int findStessayRecord(String hql) { 3 // TODO Auto-generated method stub 4 5 return stessayDao.getAllRowCount(hql); // 總記錄數 6 } 7 @Override 8 public List<Stessay> queryForPage(Stessay stessay, String hql, int offset, 9 int length) { 10 // TODO Auto-generated method stub 11 return stessayDao.queryForPage(hql, offset, length); 12 }
Dao接口只有方法,不寫具體實現:
1 public List<Stessay> findAll(); 2 public int getAllRowCount(String hql); 3 public List<Stessay> queryForPage(final String hql, final int offset, final int length) ;
具體方法的實現交給DaoImpl:
1 /** 2 * 查詢所有的記錄數 3 * @param hql 查詢條件 4 * @return 總記錄數 5 */ 6 @Override 7 public int getAllRowCount(String hql) { 8 return this.getHibernateTemplate().find(hql).size(); 9 } 10 /** 11 * 分頁查詢 12 * @param hql 查詢條件 13 * @param offset 開始記錄 14 * @param length 一次查詢幾條記錄 15 * @return 查詢的記錄集合 16 */ 17 @Override 18 public List<Stessay> queryForPage(final String hql, final int offset, final int length) { 19 Session session = this.getSession(); 20 Query q = session.createQuery(hql); 21 q.setFirstResult(offset); 22 q.setMaxResults(length); 23 List<Stessay> list = q.list(); 24 25 System.out.println("--------PaperImpl---------------size() "+list.size()); 26 session.close(); 27 return list; 28 }
真分頁就是頁面顯示多少,就從數據庫里加載多少出來,這樣一來就會提高效率。假分頁就是將數據庫里面所有的數據全部加載出來,但是只顯示需要顯示的部分,對於數據特別多的系統,這樣下來,效率就會特別低。