在進行web應用開發的時候經常要對Struts分頁處理
一
這種方法對於在數據量少的時候很方便
這種結構對於數據量大的程序很好
二
由於在Hibernate中直接提供了對數據庫定點定量的查詢方法
如
從第
Query q = session
q
q
List l = q
三
package com
import java
public class Pager {
private int totalRows; //總行數
private int pageSize =
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>
totalPages++;
}
currentPage =
startRow =
}
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
}
public void setStartRow(int startRow) {
this
}
public void setTotalPages(int totalPages) {
this
}
public void setCurrentPage(int currentPage) {
this
}
public void setPageSize(int pageSize) {
this
}
public int getTotalRows() {
return totalRows;
}
public void first() {
currentPage =
startRow =
}
public void previous() {
if (currentPage ==
return;
}
currentPage
startRow = (currentPage
}
public void next() {
if (currentPage < totalPages) {
currentPage++;
}
startRow = (currentPage
}
public void last() {
currentPage = totalPages;
startRow = (currentPage
}
public void refresh(int _currentPage) {
currentPage = _currentPage;
if (currentPage > totalPages) {
last();
}
}
Pager類用於計算首頁
package com
import javax
public class PagerHelper {
public static Pager getPager(HttpServletRequest httpServletRequest
int totalRows) {
//定義pager對象
Pager pager = new Pager(totalRows);
//從Request對象中獲取當前頁號
String currentPage = (
//如果當前頁號為空
//如果不為空
if (currentPage != null) {
pager
}
//獲取當前執行的方法
String pagerMethod = (
if (pagerMethod != null) {
if (pagerMethod
pager
} else if (pagerMethod
pager
} else if (pagerMethod
pager
} else if (pagerMethod
pager
}
}
return pager;
}
PageHelper這個類
package com
import com
import com
import net
import java
import ntroller
public class VehiclePropertyDAO {
public Collection findWithPage(int pageSize
HibernateException {
Collection vehicleList = null;
Transaction tx = null;
try {
Session session = HibernateUtil
tx = session
Query q = session
q
q
vehicleList = q
mit();
} catch (HibernateException he) {
if (tx != null) {
tx
}
throw he;
} finally {
HibernateUtil
}
return vehicleList;
}
public int getRows(String query) throws
HibernateException {
int totalRows =
Transaction tx = null;
try {
Session session = HibernateUtil
tx = session
totalRows = ((Integer) erate(query)
intValue();
mit();
} catch (HibernateException he) {
if (tx != null) {
tx
}
throw he;
} finally {
HibernateUtil
}
return totalRows;
}
DAO類我就貼這些分頁需要的代碼了
下面是在Action中用到的代碼
public ActionForward queryWithPage(ActionMapping actionMapping
ActionForm actionForm
HttpServletRequest httpServletRequest
HttpServletResponse httpServletresponse) {
Collection clInfos = null;//用於輸出到頁面的記錄集合
int totalRows;//記錄總行數
VehiclePropertyDAO vehicleDAO = new VehiclePropertyDAO();
//取得當前表中的總行數
try {
totalRows = vehicleDAO
} catch (Exception ex) {
servlet
return actionMapping
}
//通過PagerHelper類來獲取用於輸出到頁面的pager對象
Pager pager=PagerHelper
//取出從startRow開始的pageSize行記錄
try {
clInfos = vehicleDAO
} catch (Exception ex) {
servlet
return actionMapping
}
//把輸出的記錄集和pager對象保存到request對象中
(
(
return actionMapping
查詢語句select count(*) from VehicleProperty 也可以換成你需要的任意的條件(select count(*) from VehicleProperty where
下面就是在JSP中的應用了
=
解釋一下這一行:
method=queryWithPage
是由於我的Action繼承的是DispatchActionpageMethod=first 是用來在PageHelper類中判斷執行哪個操作
四
我做的這個也只是一個借鑒
其實最關鍵的是把當前頁號和要執行的是功能(上一頁
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28681.html