自己寫個分頁,用起來挺爽
PageBean類: 封裝一切頁面元素及結果集,保存在Session里,省略了一切能省略的setter getter
package com.yls.car.bean.car;

//通用Hibernate分頁類
import java.util.*;

import org.hibernate.Session;
import org.hibernate.Query;
import org.hibernate.HibernateException;
import org.apache.struts.util.LabelValueBean;

 public class PageBean {
private int currentPage = 0; //當前頁
private int totalPages = 0; //總頁數
private int pageRecords = 0; //每頁顯示記錄數
private int totalRecords = 0; //總記錄數
private int startRecord = 0; //當前頁的第1條記錄在數據庫中的起始行
private boolean hasNextPage = false; //是否有下一頁
private boolean hasPreviousPage = false; //是否有上一頁
private List resultList = null; //返回的分頁結果集
private Query query = null;
private ArrayList pages = new ArrayList(); //對應頁面的html:select框
private int selectPage = 0; //對應頁面所選的頁面
 public PageBean() {
}
 public PageBean(String hql,Session session,int _pageRecords) throws HibernateException {
//第一次分頁,給類屬性賦值
query = session.createQuery(hql);
currentPage = 1;
pageRecords = _pageRecords;
totalRecords = query.list().size();
 if (totalRecords % pageRecords == 0) {
totalPages = totalRecords/pageRecords;
 } else {
totalPages = totalRecords/pageRecords + 1;
}
startRecord = 0;
setHasPreviousPage();
setHasNextPage();
setPages();
dividePage();
}
//分頁并將結果集做為PageBean一個屬性
 public void dividePage() throws HibernateException {
query.setFirstResult(startRecord);
query.setMaxResults(pageRecords);
resultList = query.list();
}
//判斷動作
 public void judgeAction(String action) {
 if (action.equals("firstPage")) {
currentPage = 1;
startRecord = 0;
setHasPreviousPage();
setHasNextPage();
dividePage();
 } else if (action.equals("previousPage") && currentPage-1>0) {
currentPage = currentPage-1;
setStartRecord();
setHasPreviousPage();
setHasNextPage();
dividePage();
 } else if (action.equals("nextPage") && currentPage<totalPages) {
currentPage = currentPage+1;
setStartRecord();
setHasPreviousPage();
setHasNextPage();
dividePage();
 } else if (action.equals("lastPage")) {
currentPage = totalPages;
setStartRecord();
setHasPreviousPage();
setHasNextPage();
dividePage();
 } else if (isNumber(action)) {
currentPage = Integer.parseInt(action);
setStartRecord();
setHasPreviousPage();
setHasNextPage();
dividePage();
}
}
//判斷是否是數字
 public boolean isNumber(String number) {
 try {
Integer.parseInt(number);
return true;
 } catch (Exception e) {
e.printStackTrace();
return false;
}
}
//返回當前頁
 public int getCurrentPage() {
return currentPage;
}
//獲得是否有下頁,用于頁面是否顯示"下一頁"
 public boolean isHasNextPage() {
return hasNextPage;
}
//設置是否有下頁
 private void setHasNextPage() {
 if (currentPage < totalPages) {
hasNextPage = true;
 } else {
hasNextPage = false;
}
}
//獲得是否有上頁,用于頁面是否顯示"上一頁"
 public boolean isHasPreviousPage() {
return hasPreviousPage;
}
//設置是否有上頁
 private void setHasPreviousPage() {
 if (currentPage-1 > 0) {
hasPreviousPage = true;
 } else {
hasPreviousPage = false;
}
}
//設置從哪行取記錄
 public void setStartRecord() {
startRecord = (currentPage-1)*pageRecords;
}
//獲得總頁數
 public int getTotalPages() {
return totalPages;
}
//返回結果集給頁面的迭代器
 public List getResultList() {
return resultList;
}
//返回集合給html:optionsCollection
 public ArrayList getPages() {
return pages;
}
//設置頁面下拉框的集合
 public void setPages() {
 for(int i=0;i<totalPages;i++) {
pages.add(new LabelValueBean(String.valueOf(i+1),String.valueOf(i+1)));
}
}
//獲取所選的頁面
 public int getSelectPage() {
return selectPage;
}
}


Action中調用(只需傳入3個參數hibernate中的session,HQL,還有每頁分多少行數據,一切搞定)
public ActionForward execute(ActionMapping mapping, ActionForm form,
 HttpServletRequest request, HttpServletResponse response) {
Session ss = null;
HttpSession session = null;
PageBean page = null;
 try {
ss = HibernateSessionFactory.getSession();
session = request.getSession();
String action = request.getParameter("action");
String selectPage = request.getParameter("selectPage");
 if (action == null && selectPage == null) {
page = new PageBean("FROM YcAccident", ss, 10);
page.dividePage();
session.setAttribute("page", page);
 } else {
 if (session.getAttribute("page") != null && action != null) {
page = (PageBean) session.getAttribute("page");
page.judgeAction(action);
} else if (session.getAttribute("page") != null
 && selectPage != null) {
page = (PageBean) session.getAttribute("page");
page.judgeAction(selectPage);
}
}
 } catch (Exception e) {
e.printStackTrace();
}
return mapping.findForward("querySuccess");
}
頁面顯示
<table border="1">
<tr>
<td>流水號</td>
<td>類型</td>
<td>我方車牌號</td>
<td>我方駕駛者</td>
<td>對方車牌號</td>
<td>對方駕駛者</td>
<td>發生時間</td>
<td>發生地點</td>
<td>處理交警</td>
<td align="center">操作</td>
</tr>
<logic:iterate name="page" property="list" id="YcAccident"
type="com.yls.car.bean.hibernate.YcAccident">
<tr>
<td><bean:write name="YcAccident" property="aid" /></td>
<td><bean:write name="YcAccident" property="type" /></td>
<td><bean:write name="YcAccident" property="myCarSign" /></td>
<td><bean:write name="YcAccident" property="myDriver" /></td>
<td><bean:write name="YcAccident" property="oppCarSign" /></td>
<td><bean:write name="YcAccident" property="oppDriver" /></td>
<td><bean:write name="YcAccident" property="occurTime" /></td>
<td><bean:write name="YcAccident" property="occurAddress" /></td>
<td><bean:write name="YcAccident" property="disposer" /></td>
<td>
<input type="submit" value="刪除" onClick="del(<bean:write name="YcAccident" property="aid" />)">
<input type="button" value="詳細" onClick="modify('<bean:write name="YcAccident" property="aid" />',
'<bean:write name="YcAccident" property="type" />',
'<bean:write name="YcAccident" property="myCarSign" />',
'<bean:write name="YcAccident" property="myDriver" />',
'<bean:write name="YcAccident" property="oppCarSign" />',
'<bean:write name="YcAccident" property="oppDriver" />',
'<bean:write name="YcAccident" property="oppCarModel" />',
'<bean:write name="YcAccident" property="oppOrg" />',
'<bean:write name="YcAccident" property="occurTime" />',
'<bean:write name="YcAccident" property="occurAddress" />',
'<bean:write name="YcAccident" property="disposeDepart" />',
'<bean:write name="YcAccident" property="disposer" />',
'<bean:write name="YcAccident" property="scene" />',
'<bean:write name="YcAccident" property="damnify" />',
'<bean:write name="YcAccident" property="disposeResult" />',
'<bean:write name="YcAccident" property="myResponsibility" />',
'<bean:write name="YcAccident" property="punishAmount" />',
'<bean:write name="YcAccident" property="myAmount" />',
'<bean:write name="YcAccident" property="oppAmount" />',
'<bean:write name="YcAccident" property="insuranceAmount" />')">
</td>
</tr>
</logic:iterate>
<tr>
<td align="right" colspan="10">
<logic:equal name="page" property="hasPreviousPage" value="true">
<html:link page="/showAccident.do?action=previousPage">上一頁</html:link>
</logic:equal>
<logic:equal name="page" property="hasNextPage" value="true">
<html:link page="/showAccident.do?action=nextPage">下一頁</html:link>
</logic:equal>
總頁數:<bean:write name="page" property="totalPages" />
當前頁:<bean:write name="page" property="currentPage" />
跳轉到:<html:select name="page" property="selectPage" onchange="pageNumber(this)">//這里獲得頁碼,提交到Action
<html:optionsCollection name="page" property="pages" label="label" value="value"/>
</html:select>
</td>
</tr>
</table>
效果圖:
|