程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Hibernate實現Struts分頁中的分頁處理

Hibernate實現Struts分頁中的分頁處理

編輯:關於JAVA

在進行web應用開發的時候經常要對Struts分頁處理,經常看到一些人在問Struts分頁處理的問題,現在我把自己的處理方法寫在這兒,希望能對需要進行Struts分頁處理的朋友有所幫助。

一、在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中的應用了:

="/bussiness/clInfo/queryWithPage.do?method=queryWithPage&pageMethod=first
" paramName="PAGER" paramProperty="currentPage" paramId="currentPage">首頁

解釋一下這一行:"/bussiness/clInfo/queryWithPage.do?method=queryWithPage&pageMethod=first

method=queryWithPage

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved