java 代码
- 这里我以从一个用户表中查询用户信息为例演示其用法:
- 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
- {
- UserEO userEO=(UserEO)usersList.get(i);%>
- <%=userEO.getUsername()%>
- <%=userEO.getName()%>
- <%=userEO.getPhoneNumber()%>
- <%=userEO.getEmailBox()%>
- <%=userEO.getAddress()%>
- <%=userEO.getPostcode()%>
- <%}%>
- <%=pageResultSet.getToolBar("list_users.do")%>
- 注意:
- 1、如果你觉得分页工具栏不能满足你的要求,
- q网管(Evp;育H9p,
- 可以用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.*;
- /**
- *
- Title: PageResultSet
- *
- *
- Description:分页类
- *
- *
- Copyright: Copyright (c) 2004
- *
- *
- Company:youngor-studio(http://www.54youngor.com)
- * @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:
- .vf]Y5{p}BuC"W
- 返回分页大小
- *
- * @return int
- */
- public int getPageSize() {
- return pageSize;
- }
- /**
- * getRowsCount:8NsAX教t$nT"TQ返回总记录行数
- *
- * @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="
- ";
- str+="
- ";
- if(isFirst())
- str+="首页 上一页 ";
- else
- {
- str+="首页 ";
- str+="上一页 ";
- }
- if(isLast())
- str+="下一页 尾页 ";
- else
- {
- str+="下一页 ";
- str+="尾页 ";
- }
- str+=" 共"+rowsCount+"条记录 ";
- str+=" 转到
- ";
- return str;
- }
- }