這里我以從一個用戶表中查詢用戶信息為例演示其用法:
1.將PageResultSet.java文件編譯成class文件,并放入你的Web
應用程序的WEB-INF/classes/com/youngor/util目錄下,可以對包名做相應修改。
2.在你的Action類中:
先從業務處理邏輯類中取出數據(ArrayList或Vector格式)
UserBO userBO=new UserBO();
Collection data=userBO.findUsers();//示例方法
再得到當前頁curPage和每頁記錄數pageSize
int curPage = Integer.parseInt(request.getParameter(“cur_page“));
int pageSize=15;
然后生成PageResultSet對象
PageResultSet dataList = new PageResultSet(data, curPage, pageSize);
request.setAttribute("usersList", usersList);
3.在你的JSP頁面中:
?? <%
????????? PageResultSet pageResultSet=(PageResultSet)request.getAttribute("usersList");
?? ArrayList usersList=(ArrayList)pageResultSet.getData();
?? for(int i=0;i<usersList.size();i++)
?? {
??????????? UserEO userEO=(UserEO)usersList.get(i);%>
????? <tr>
??????? <td><a href="view_user.do?id=<%=userEO.getId()%>"><%=userEO.getUsername()%></a></td>
??????? <td><%=userEO.getName()%></td>
??????? <td><%=userEO.getPhoneNumber()%></td>
??????? <td><%=userEO.getEmailBox()%></td>
??????? <td><%=userEO.getAddress()%></td>
??????? <td><%=userEO.getPostcode()%></td>
??????? </tr>
??? <%}%>
??? </table></td>
? </tr>
</table>
<!-- 顯示分頁工具欄 -->
<%=pageResultSet.getToolBar("list_users.do")%>
注意:
1、如果你覺得分頁工具欄不能滿足你的要求,可以用PageResultSet類中的公共方法
first()、previous()、next()、last()定制自己的工具欄,并且,你還可以在PageResultSet中定義多個樣式的工具欄;
2、getToolBar(String url)方法接受帶查詢字符串的參數,比如“list_users.do?class_id=1“。
//PageResultSet.java
package com.youngor.util;
import java.util.*;
/**
?* <p>Title: PageResultSet</p>
?*
?* <p>Description:分頁類 </p>
?*
?* <p>Copyright: Copyright (c) 2004</p>
?*
?* <p>Company:youngor-studio(http://www.54youngor.com) </p>
?* @author:伍維波
?* @version 1.0
?*/
public class PageResultSet {
??? /**
???? * 分頁數據
???? */
??? private Collection data = null;
??? /**
???? * 當前頁
???? */
??? private int curPage;
??? /**
???? * 每頁顯示的記錄數
???? */
??? private int pageSize;
??? /**
???? * 記錄行數
???? */
??? private int rowsCount;
??? /**
???? * 頁數
???? */
??? private int pageCount;
??? public PageResultSet(Collection data) {
??????? this.data = data;
??????? this.curPage = 1;
??????? this.pageSize = 10;
??????? this.rowsCount = data.size();
??????? this.pageCount = (int) Math.ceil((double) rowsCount / pageSize);
??? }
??? public PageResultSet(Collection data, int curPage) {
??????? this.data = data;
??????? this.curPage = curPage;
??????? this.pageSize = 10;
??????? this.rowsCount = data.size();
??????? this.pageCount = (int) Math.ceil((double) rowsCount / pageSize);
??? }
??? public PageResultSet(Collection data, int curPage, int pageSize) {
??????? this.data = data;
??????? this.curPage = curPage;
??????? this.pageSize = pageSize;
??????? this.rowsCount = data.size();
??????? this.pageCount = (int) Math.ceil((double) rowsCount / pageSize);
??? }
??? /**
???? * getCurPage:返回當前的頁數
???? *
???? * @return int
???? */
??? public int getCurPage() {
??????? return curPage;
??? }
??? /**
???? * getPageSize:返回分頁大小
???? *
???? * @return int
???? */
??? public int getPageSize() {
??????? return pageSize;
??? }
??? /**
???? * getRowsCount:返回總記錄行數
???? *
???? * @return int
???? */
??? public int getRowsCount() {
??????? return rowsCount;
??? }
??? /**
???? * getPageCount:返回總頁數
???? *
???? * @return int
???? */
??? public int getPageCount() {
??????? return pageCount;
??? }
??? /**
???? * 第一頁
???? * @return int
???? */
??? public int first() {
??????? return 1;
??? }
??? /**
???? * 最后一頁
???? * @return int
???? */
??? public int last() {
??????? return pageCount;
??? }
??? /**
???? * 上一頁
???? * @return int
???? */
??? public int previous() {
??????? return (curPage - 1 < 1) ? 1 : curPage - 1;
??? }
??? /**
???? * 下一頁
???? * @return int
???? */
??? public int next() {
??????? return (curPage + 1 > pageCount) ? pageCount : curPage + 1;
??? }
??? /**
???? * 第一頁
???? * @return boolean
???? */
??? public boolean isFirst() {
??????? return (curPage==1)?true:false;
??? }
??? /**
???? * 第一頁
???? * @return boolean
???? */
??? public boolean isLast() {
??????? return (curPage==pageCount)?true:false;
??? }
??? /**
???? * 獲取當前頁數據
???? * @return Collection
???? */
??? public Collection getData() {
??????? Collection curData = null;
??????? if (data != null) {
??????????? int start = (curPage - 1) * pageSize;
??????????? int end = 0;
??????????? if (start + pageSize > rowsCount)
??????????????? end = rowsCount;
??????????? else
??????????????? end = start + pageSize;
??????????? ArrayList arrayCurData = new ArrayList();
??????????? ArrayList arrayData = null;
??????????? Vector vectorCurData = new Vector();
??????????? Vector vectorData = null;
??????????? boolean isArray = true;
??????????? if (data instanceof ArrayList) {
??????????????? arrayData = (ArrayList) data;
??????????????? isArray = true;
??????????? } else if (data instanceof Vector) {
??????????????? vectorData = (Vector) data;
??????????????? isArray = false;
??????????? }
??????????? for (int i = start; i < end; i++) {
??????????????? if (isArray) {
??????????????????? arrayCurData.add(arrayData.get(i));
??????????????? } else {
??????????????????? vectorData.add(vectorData.elementAt(i));
??????????????? }
??????????? }
??????????? if (isArray) {
??????????????? curData = (Collection) arrayCurData;
??????????? } else {
??????????????? curData = (Collection) vectorCurData;
??????????? }
??????? }
??????? return curData;
??? }
??? /**
???? * 獲取工具條
???? * @return String
???? */
??? public String getToolBar(String fileName){
??????? String temp="";
??????? if(fileName.indexOf("?")==-1)
??????? {
??????????? temp="?";
??????? }
??????? else
??????? {
??????????? temp="&";
??????? }
??????? String str="<form method='post' name='frmPage' action='"+fileName+"'>";
??????? str+="<p align='center'>";
??????? if(isFirst())
??????????? str+="首頁 上一頁 ";
??????? else
??????? {
??????????? str+="<a href='"+fileName+temp+"cur_page=1'>首頁</a> ";
??????????? str+="<a href='"+fileName+temp+"cur_page="+(curPage-1)+"'>上一頁</a> ";
??????? }
??????? if(isLast())
??????????? str+="下一頁 尾頁 ";
??????? else
??????? {
??????????? str+="<a href='"+fileName+temp+"cur_page="+(curPage+1)+"'>下一頁</a> ";
??????????? str+="<a href='"+fileName+temp+"cur_page="+pageCount+"'>尾頁</a> ";
??????? }
??????? str+=" 共<b>"+rowsCount+"</b>條記錄 ";
??????? str+=" 轉到<select name='page' onChange=\"location='"+fileName+temp+"cur_page='+this.options[this.selectedIndex].value\">";
??????? for(int i=1;i<=pageCount;i++)
??????? {
??????????? if(i==curPage)
??????????????? str+="<option value='"+i+"' selected>第"+i+"頁</option>";
??????????? else
??????????????? str+="<option value='"+i+"'>第"+i+"頁</option>";
??????? }
??????? str+="</select></p></form>";
??????? return str;
??? }
}