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

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


Action中調(diào)用(只需傳入3個(gè)參數(shù)hibernate中的session,HQL,還有每頁(yè)分多少行數(shù)據(jù),一切搞定)
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");
}
頁(yè)面顯示
<table border="1">
<tr>
<td>流水號(hào)</td>
<td>類型</td>
<td>我方車牌號(hào)</td>
<td>我方駕駛者</td>
<td>對(duì)方車牌號(hào)</td>
<td>對(duì)方駕駛者</td>
<td>發(fā)生時(shí)間</td>
<td>發(fā)生地點(diǎn)</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="詳細(xì)" 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">上一頁(yè)</html:link>
</logic:equal>
<logic:equal name="page" property="hasNextPage" value="true">
<html:link page="/showAccident.do?action=nextPage">下一頁(yè)</html:link>
</logic:equal>
總頁(yè)數(shù):<bean:write name="page" property="totalPages" />
當(dāng)前頁(yè):<bean:write name="page" property="currentPage" />
跳轉(zhuǎn)到:<html:select name="page" property="selectPage" onchange="pageNumber(this)">//這里獲得頁(yè)碼,提交到Action
<html:optionsCollection name="page" property="pages" label="label" value="value"/>
</html:select>
</td>
</tr>
</table>
效果圖:
|