2.分页计算类Pages:
package com.fellow.pub.util;import java.util.Vector;
import java.util.Collection;
import com.fellow.pub.servlet.IFormPages;public class Pages {
  protected int itemCount;
  protected int itemPerPage;
  protected int currentPage;  public Pages() {
    this.itemCount = 0;
    this.itemPerPage = 0;
    this.currentPage = 1;
  }  public Pages(int itemCount, int itemPerPage) {
    this.itemCount = itemCount;
    this.itemPerPage = itemPerPage;
    this.currentPage = 1;
  }  public Pages(int itemCount, int itemPerPage, int currentPage) {
    this.itemCount = itemCount;
    this.itemPerPage = itemPerPage;
    this.currentPage = currentPage;
  }  public Pages(Collection collection, IFormPages formPages) {
    this.itemCount = collection.size();
    this.itemPerPage = formPages.getItemPerPage();
    this.currentPage = formPages.getCurrentPage();
  }  public int getItemCount() {
    return this.itemCount;
  }  public void setItemCount(int itemCount) {
    this.itemCount = itemCount;
  }  public int getItemPerPage() {
    return this.itemPerPage;
  }  public void setItemPerPage(int itemPerPage) {
    this.itemPerPage = itemPerPage;
  }  public int getCurrentPage() {
    return this.currentPage;
  }  public void setCurrentPage(int currentPage) {
    this.currentPage = currentPage;
  }  public int getPrePage(){
    return this.currentPage - 1;
  }  public int getNextPage(){
    return this.currentPage + 1;
  }  protected void AssertErrors() throws Exception{
    if(itemPerPage <= 0 ||
       currentPage <= 0 ||
       itemCount < itemPerPage * (currentPage - 1)){
      throw new Exception("Pages object calculated error");
    }
  }  public int getPageCount() throws Exception{
    if(itemCount <= 0) return 0;
    AssertErrors();
    if(itemCount % itemPerPage == 0){
      return itemCount / itemPerPage;
    }
    else{
      return itemCount / itemPerPage + 1;
    }
  }  public int getPageItemCount() throws Exception{
    if(itemCount <= 0) return 0;
    int pageCount = getPageCount();    if(pageCount == currentPage){
      return itemCount -itemPerPage * (currentPage - 1);
    }
    else{
      return itemPerPage;
    }
  }  public int getPageItemCount(int currentPage) throws Exception{
    if(itemCount <= 0) return 0;
    int pageCount = getPageCount();    if(pageCount == currentPage){
      return itemCount -itemPerPage * (currentPage - 1);
    }
    else{
      return itemPerPage;
    }
  }  public boolean isFirstPage() throws Exception{
    return (currentPage == 1);
  }  public boolean isLastPage() throws Exception{
    int pageCount = getPageCount();
    return (pageCount == currentPage);
  }  public Collection getIndexList() throws Exception{
    int pageCount = getPageCount();
    Vector result = new Vector(pageCount);
    for(int i = 0; i < pageCount; i++){
      result.add(new Integer(i + 1));
    }
    return result;
  }
}

解决方案 »

  1.   

    3.分页集合类CollectionPages,继承Pages:
    package com.fellow.pub.util;import java.util.*;import com.fellow.pub.servlet.IFormPages;public class CollectionPages extends Pages {
      protected Collection collection = null;
      protected Collection collectionPage = null;
      public CollectionPages(Collection collection, IFormPages formPages) throws Exception {
        super(collection, formPages);
        this.collection = collection;
        buildCollectionPage();
      }  public Collection getCollection(){
        return this.collection;
      }  public void setCollection(Collection collection){
        this.collection = collection;
      }  public Collection getCollectionPage(){
        return this.collectionPage;
      }  public Collection buildCollectionPage() throws Exception{
        if(collection == null){
          throw new Exception("collection object is null");
        }    collectionPage = new Vector(itemPerPage);
        CollectionUtil.copy(collection, collectionPage,
                            (currentPage - 1) * itemPerPage,
                            (currentPage) * itemPerPage);
        return collectionPage;
      }
    }
      

  2.   

    4.Action里面初始化:
    CollectionPages collectionPages = new CollectionPages(collection, formPages);
    其中collection就是需要分页的集合5.保存到request
    setAttribute(name, collectionPages);6.在页面上使用collectionPages对象,collectionPages对象有两个子对象:
      i.collectionPage,就是分页以后的集合
        <bean:define id="collectionList" name="collectionPages" property="collectionPage" scope="request" /> 
      ii.indexList就是页码(由于我不知道怎么用简单的方法处理页码,就只好弄这样一个对象来iterate了)
        <bean:define id="indexList" name="collectionPages" property="indexList" scope="request"/>
      

  3.   

    能写下具体在jsp页面怎么用吗??