|
在進行web應用開發的時候經常要進行分頁處理,經??吹揭恍┤嗽趩柗猪撎幚淼膯栴},現在我把自己的處理方法寫在這兒,希望能對需要進行分頁處理的朋友有所幫助。 一、在struts中分頁有兩種結構: 1. 在Action中通過DAO查詢出所有的記錄,然后加到session或request對象中,傳到客戶端,由JSP進行分頁。 這種方法對于在數據量少的時候很方便,也不影響速度。 2.在Action中每次通過DAO只查詢出一頁的記錄,再傳給JSP頁面。 這種結構對于數據量大的程序很好,但對于數據量小的情況,會增加對服務器的請求,加大服務器的負載。 二、Hibernate查詢 由于在Hibernate中直接提供了對數據庫定點定量的查詢方法,所以我采用的是第2種方法。 如: 從第1萬條開始取出100條記錄 Query q = session.createQuery("from Cat as c"); q.setFirstResult(10000); q.setMaxResults(100); List l = q.list(); 三、具體實現 1.Pager類 package com.jpcf.db.helper; import java.math.*; public class Pager { private int totalRows; //總行數 private int pageSize = 10; //每頁顯示的行數 private int currentPage; //當前頁號 private int totalPages; //總頁數 private int startRow; //當前頁在數據庫中的起始行 public Pager() { } public Pager(int _totalRows) { totalRows = _totalRows; totalPages=totalRows/pageSize; int mod=totalRows%pageSize; if(mod>0){ totalPages++; } currentPage = 1; startRow = 0; } public int getStartRow() { return startRow; } public int getTotalPages() { return totalPages; } public int getCurrentPage() { return currentPage; } public int getPageSize() { return pageSize; } public void setTotalRows(int totalRows) { this.totalRows = totalRows; } public void setStartRow(int startRow) { this.startRow = startRow; } public void setTotalPages(int totalPages) { this.totalPages = totalPages; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalRows() { return totalRows; } public void first() { currentPage = 1; startRow = 0; } public void previous() { if (currentPage == 1) { return; } currentPage--; startRow = (currentPage - 1) * pageSize; } public void next() { if (currentPage < totalPages) { currentPage++; } startRow = (currentPage - 1) * pageSize; } public void last() { currentPage = totalPages; startRow = (currentPage - 1) * pageSize; } public void refresh(int _currentPage) { currentPage = _currentPage; if (currentPage > totalPages) { last(); } } } Pager類用于計算首頁、前一頁、下一頁、尾頁的在數據庫中的起始行,當前的頁碼。 2.PagerHelp類 package com.jpcf.db.helper; import javax.servlet.http.*; public class PagerHelper { public static Pager getPager(HttpServletRequest httpServletRequest, int totalRows) { //定義pager對象,用于傳到頁面 Pager pager = new Pager(totalRows); //從Request對象中獲取當前頁號 String currentPage = httpServletRequest.getParameter("currentPage"); //如果當前頁號為空,表示為首次查詢該頁 //如果不為空,則刷新pager對象,輸入當前頁號等信息 if (currentPage != null) { pager.refresh(Integer.parseInt(currentPage)); } //獲取當前執行的方法,首頁,前一頁,后一頁,尾頁。 String pagerMethod = httpServletRequest.getParameter("pageMethod"); if (pagerMethod != null) { if (pagerMethod.equals("first")) { pager.first(); } else if (pagerMethod.equals("previous")) { pager.previous(); } else if (pagerMethod.equals("next")) { pager.next(); } else if (pagerMethod.equals("last")) { pager.last(); } } return pager; } } PageHelper這個類,我不用說應該也知道用來干嘛了 3.DAO類 package com.jpcf.db.dao; import com.jpcf.db.model.*; import com.jpcf.db.helper.HibernateUtil; import net.sf.hibernate.*; import java.util.*; import com.jpcf.db.controller.*; public class VehiclePropertyDAO { public Collection findWithPage(int pageSize, int startRow) throws HibernateException { Collection vehicleList = null; Transaction tx = null; try { Session session = HibernateUtil.currentSession(); tx = session.beginTransaction(); Query q = session.createQuery("from VehicleProperty vp"); q.setFirstResult(startRow); q.setMaxResults(pageSize); vehicleList = q.list(); tx.commit(); } catch (HibernateException he) { if (tx != null) { tx.rollback(); } throw he; } finally { HibernateUtil.closeSession(); } return vehicleList; } public int getRows(String query) throws HibernateException { int totalRows = 0; Transaction tx = null; try { Session session = HibernateUtil.currentSession(); tx = session.beginTransaction(); totalRows = ((Integer) session.iterate(query).next()). intValue(); tx.commit(); } catch (HibernateException he) { if (tx != null) { tx.rollback(); } throw he; } finally { HibernateUtil.closeSession(); } return totalRows; } } DAO類我就貼這些分頁需要的代碼了。 “from VehicleProperty vp”也可以用一個參數傳進來,有興趣的自己改一下吧 4.Action 下面是在Action中用到的代碼:/ public ActionForward queryWithPage(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletresponse) { Collection clInfos = null;//用于輸出到頁面的記錄集合 int totalRows;//記錄總行數 VehiclePropertyDAO vehicleDAO = new VehiclePropertyDAO(); //取得當前表中的總行數 try { totalRows = vehicleDAO.getRows("select count(*) from VehicleProperty"); } catch (Exception ex) { servlet.log(ex.toString()); return actionMapping.findForward(Constants.FAILURE); } //通過PagerHelper類來獲取用于輸出到頁面的pager對象 Pager pager=PagerHelper.getPager(httpServletRequest,totalRows); //取出從startRow開始的pageSize行記錄 try { clInfos = vehicleDAO.findWithPage(pager.getPageSize(), pager.getStartRow()); } catch (Exception ex) { servlet.log(ex.toString()); return actionMapping.findForward(Constants.FAILURE); } //把輸出的記錄集和pager對象保存到request對象中 httpServletRequest.setAttribute("CLINFOS", clInfos); httpServletRequest.setAttribute("PAGER", pager); return actionMapping.findForward(Constants.SUCCESS); } 查詢語句select count(*) from VehicleProperty 也可以換成你需要的任意的條件(select count(*) from VehicleProperty where ..) 5.JSP頁面使用 下面就是在JSP中的應用了: <td colspan="8" align="right" class="head"> 第<bean:write name="PAGER" property="currentPage"/>頁 共<bean:write name="PAGER" property="totalPages"/>頁 <html:link action="/bussiness/clInfo/queryWithPage.do?method=queryWithPage&pageMethod=first" paramName="PAGER" paramProperty="currentPage" paramId="currentPage">首頁</html:link> <html:link action="/bussiness/clInfo/queryWithPage.do?method=queryWithPage&pageMethod=previous" paramName="PAGER" paramProperty="currentPage" paramId="currentPage">上一頁</html:link> <html:link action="/bussiness/clInfo/queryWithPage.do?method=queryWithPage&pageMethod=next" paramName="PAGER" paramProperty="currentPage" paramId="currentPage">下一頁</html:link> <html:link action="/bussiness/clInfo/queryWithPage.do?method=queryWithPage&pageMethod=last" paramName="PAGER" paramProperty="currentPage" paramId="currentPage">尾頁</html:link> </td> 解釋一下這一行:"/bussiness/clInfo/queryWithPage.do?method=queryWithPage&pageMethod=first method=queryWithPage 是由于我的Action繼承的是DispatchAction,需要一個method參數 pageMethod=first 是用來在PageHelper類中判斷執行哪個操作 四、總結 我做的這個也只是一個借鑒,還有很多沒有實現的,比如還可以加一下 go 直接到第n頁的功能。 其實最關鍵的是把當前頁號和要執行的是功能(上一頁,下一頁)的參數從頁面傳進來,在Action中就可以根據這兩個參數去取下一個頁面上要顯示的記錄集了 |