關于Struts+Hibernate分頁的問題
在進行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");
|
三、具體實現
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, |
//定義pager對象,用于傳到頁面
Pager pager = new Pager(totalRows); |
//從Request對象中獲取當前頁號
String currentPage = httpServletRequest.getParameter("currentPage"); |
//如果當前頁號為空,表示為***查詢該頁
//如果不為空,則刷新pager對象,輸入當前頁號等信息
if (currentPage != null) { |
//獲取當前執行的方法,首頁,前一頁,后一頁,尾頁。
String pagerMethod = httpServletRequest.getParameter("pageMethod"); if (pagerMethod != null) { |
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, |
Collection clInfos = null;//用于輸出到頁面的記錄集合
int totalRows;//記錄總行數
VehiclePropertyDAO vehicleDAO = new VehiclePropertyDAO(); |
//取得當前表中的總行數
try { |
//通過PagerHelper類來獲取用于輸出到頁面的pager對象
Pager pager=PagerHelper.getPager(httpServletRequest,totalRows); |
//取出從startRow開始的pageSize行記錄
try { |
//把輸出的記錄集和pager對象保存到request對象中
httpServletRequest.setAttribute("CLINFOS", clInfos); return actionMapping.findForward(Constants.SUCCESS); |
查詢語句select count(*) from VehicleProperty 也可以換成你需要的任意的條件(select count(*) from VehicleProperty where ..)
5.JSP頁面使用
下面就是在JSP中的應用了:
第頁 |
解釋一下這一行:"/bussiness/clInfo/queryWithPage.do?method=queryWithPage&pageMethod=first
method=queryWithPage 是由于我的Action繼承的是DispatchAction,需要一個method參數
pageMethod=first 是用來在PageHelper類中判斷執行哪個操作
四、總結
我做的這個也只是一個借鑒,還有很多沒有實現的,比如還可以加一下 go 直接到第n頁的功能。
其實最關鍵的是把當前頁號和要執行的是功能(上一頁,下一頁)的參數從頁面傳進來,在Action中就可以根據這兩個參數去取下一個頁面上要顯示的記錄集了。
【編輯推薦】