我把我们项目中(基于struts)的分页技术在这里讲一下。大家共享:
(1) 创建了分页的两个类:Page, PageInfo, NormalPage:
分别如下:
   Page.java//////////////////////////////
    /*
 * @(#) Page.java 1.00 2004/02/07
 *
 *  (c) Copyright 2003 NIPPON TELEGRAPH AND TELEPHONE EAST CORPORATION
 *
 *   FILENAME     :  Page.java
 *   PACKAGE      :  nmsgwe.common.businessinfo
 *   CREATE DATE  :  2004/02/07
 *   AUTHOR       :  Feng wei
 *   MODIFIED BY  :  Wang guangcai
 *   DESCRIPTION  :
 */
package nmsgwe.busicommon.businessinfo;/**
 * DOCUMENT ME!
 *
 * @author Feng wei
 */
public class Page {
    private int pageNum;
    private String pageStyle;    /**
     * Creates a new Page object.
     *
     * @param pageNum DOCUMENT ME, CHANGE IT!!!!!
     * @param currentPage DOCUMENT ME, CHANGE IT!!!!!
     */
    public Page(int pageNum, int currentPage) {
        this.pageNum = pageNum;
        pageStyle = (currentPage == 0) ? "2"
                                       : ((currentPage == pageNum) ? "0" : "1");
    }    /**
     * DOCUMENT ME!
     *
     * @return Returns the index.
     */
    public int getPageNum() {
        return pageNum;
    }    /**
     * DOCUMENT ME!
     *
     * @param pageNum DOCUMENT ME, CHANGE IT!!!!!
     */
    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }    /**
     * DOCUMENT ME!
     *
     * @return Returns the isUrl.
     */
    public String getPageStyle() {
        return pageStyle;
    }
}
PageInfo.java//////////////////////
   /*
 * @(#) PageInfo.java 1.00 2004/01/29
 *
 *  (c) Copyright 2003 NIPPON TELEGRAPH AND TELEPHONE EAST CORPORATION
 *
 *   FILENAME     :  PageInfo.java
 *   PACKAGE      :  nmsgwe.common.businessinfo
 *   CREATE DATE  :  2004/01/29
 *   AUTHOR       :  hpadmin
 *   MODIFIED BY  :  ?海波
 *   DESCRIPTION  :
 */
package nmsgwe.busicommon.businessinfo;import java.util.ArrayList;
import java.util.List;import nmsgwe.busicommon.exception.FletsPageNumException;/**
 * @author ?海波
 *
 */
public abstract class PageInfo {
    /*
     * Totalペ?ジ数
     */
    protected int totalPage = 0;    /*
     * ペ?ジ
     */
    protected int fromPage = 0;    /*
     * ペ?ジ
     */
    protected int toPage = 0;    /*
     * 今のペ?ジ番号
     */
    protected int currentPage = 0;    /*
     * Totalレコ?ド数
     */
    protected int totalRecord = 0;    /*
     * レコ?ド?始数
     */
    protected int fromRecord = 0;    /*
     * レコ?ド数
     */
    protected int toRecord = 0;    /*
     * 先ペ?ジ
     */
    protected int previousPage = 0;    /*
     * 後ペ?ジ
     */
    protected int nextPage = 0;    /*
     * 元表示ペ?ジ
     */
    protected int preSubmitPage = 0;    /*
     * 表示ペ?ジ数
     */
    protected int pageNum = PageConstant.DEFAULT_PAGE_NUM;    /*
     * ?のペ?ジで表示レコ?ド数
     */
    protected int pageSize = PageConstant.PAGE_SIZE;    /*
     * ?のペ?ジで表示レコ?ド数
     */
    protected List pages = new ArrayList();    /**
     * パラメ?タ?を?算する
     */
    protected abstract void computePage();    /**
     * パラメ?タ?を?算する
     * @throws FletsPageNumException exception
     */
    protected abstract void computeRecord() throws FletsPageNumException;    /**
     * 画面ペ?ジのパラメ?タ?を?算する
     * @throws FletsPageNumException exception
     */
    private void compute() throws FletsPageNumException {
        computePage();
        computeRecord();
    }

解决方案 »

  1.   

        /**
         * Constructor of PageInfo
         * @param currentPage currentPage
         * @param preSubmitPage preSubmitPage
         * @throws FletsPageNumException exception
         */
        public PageInfo(int currentPage, int preSubmitPage)
            throws FletsPageNumException {
            if (currentPage > 0) {
                this.currentPage = currentPage;
            } else {
                this.currentPage = 1;
            }
            if (preSubmitPage > 0) {
                this.preSubmitPage = preSubmitPage;
            } else {
                this.preSubmitPage = 1;
            }
            computePage();
        }    /**
         *
         * Constructor of PageInfo
         * @param currentPage currentPage
         * @throws FletsPageNumException exception
         */
        public PageInfo(int currentPage) throws FletsPageNumException {
            if (currentPage > 0) {
                this.currentPage = currentPage;
            } else {
                this.currentPage = 1;
            }        computePage();
        }    /**
         * Totalレコ?ド数を?定
         *
         * @param totalRecord totalRecord
         * @throws FletsPageNumException exception
         */
        public void setTotalRecord(int totalRecord) throws FletsPageNumException {
            this.totalRecord = totalRecord;
            if (this.pageNum != 0 && this.pageSize != 0) {
                computeRecord();
            }
        }    /**
         * 表示ペ?ジ数を?定
         *
         * @param pageNum pageNum
         * @throws FletsPageNumException exception
         */
        public void setPageNum(int pageNum) throws FletsPageNumException {
            this.pageNum = pageNum;
            //if (this.totalRecord != 0 && this.pageSize != 0) {
            if (this.pageSize != 0 && this.pageNum != 0) {
                if(this.totalRecord != 0){
                    compute();    
                } else {
                    computePage();
                }
            }
        }    /**
         * ?のペ?ジのレコ?ド数を?定
         *
         * @param pageSize pageSize
         * @throws FletsPageNumException exception
         */
        public void setPageSize(int pageSize) throws FletsPageNumException {
            this.pageSize = pageSize;
            //if (this.totalRecord != 0 && this.pageNum != 0) {
            if (this.pageSize != 0 && this.pageNum != 0) {
                if(this.totalRecord != 0){
                    compute();    
                } else {
                    computePage();
                }
            }
        }    /**
         * get current page num
         *
         * @return current page num
         */
        public int getCurrentPage() {
            return currentPage;
        }    /**
         * get next page num
         *
         * @return next page number
         */
        public int getNextPage() {
            return nextPage;
        }    /**
         * get previous page num
         *
         * @return previous page num
         */
        public int getPreviousPage() {
            return previousPage;
        }    /**
         * get total record num
         *
         * @return total record num
         */
        public int getTotalRecord() {
            return totalRecord;
        }    /**
         * get total page num
         *
         * @return total page num
         */
        public int getTotalPage() {
            return totalPage;
        }    /**
         * get the first page num in GOOGLE-style display
         *
         * @return the first page num in GOOGLE-style display
         */
        public int getFromPage() {
            return fromPage;
        }    /**
         * get the last page num in GOOGLE-style display
         *
         * @return the last page num in GOOGLE-style display
         */
        public int getToPage() {
            return toPage;
        }    /**
         * get the first record number of the range of records in current page
         *
         * @return the first record number of the range of records in current page
         */
        public int getFromRecord() {
            return fromRecord;
        }    /**
         * get the last record number of the range of records in current page
         *
         * @return int the last record number of the range of records in current
         */
        public int getToRecord() {
            return toRecord;
        }    /**
         * @return List ペ?ジレコ?ド数
         */
        public List getPages() {
            return pages;
        }    /**
         * @return int ペ?ジレコ?ド数
         */
        public int getPageSize() {
            return pageSize;
        }    /**
         * @return int ペ?ジレコ?ド数
         */
        public int getPageNum() {
            return pageNum;
        }
    }
    NNormalPage.java///////////////////
    /*
     * @(#) NormalPage.java 1.00 2004/06/1
     *
     *  (c) Copyright 2003 NIPPON TELEGRAPH AND TELEPHONE EAST CORPORATION
     *
     *   FILENAME     :  NormalPage.java
     *   PACKAGE      :  nmsgwe.busicommon.businessinfo
     *   CREATE DATE  :  2004/06/1
     *   AUTHOR       :  ?海波
     *   MODIFIED BY  :
     *   DESCRIPTION  :
     */
    package nmsgwe.busicommon.businessinfo;import nmsgwe.busicommon.exception.FletsPageNumException;
    import nmsgwe.busicommon.exception.FletsZeroPageException;
    import nmsgwe.busicommon.exception.FletsCurrentPageException;
    import nmsgwe.busicommon.exception.FletsPrePageException;
    /**
     * @author yanha
     *
     * To change the template for this generated type comment go to
     * Window>Preferences>Java>Code Generation>Code and Comments
     */
    public class NormalPage extends PageInfo {
        public NormalPage(int currentPage,int totalRecord) throws FletsPageNumException{
            super(currentPage,totalRecord);
        }
        
        public NormalPage(int currentPage) throws FletsPageNumException{
            super(currentPage);
        }
        
        public void computePage(){
            previousPage = currentPage-1;
            fromPage = (currentPage < pageNum + 1) ? 1 : (currentPage - 1)/pageNum * pageNum + 1;
            fromRecord = (currentPage - 1) * pageSize + 1;
        }    public void computeRecord() throws FletsPageNumException{
            if(totalRecord == 0){
                throw new FletsZeroPageException("total page size is ",0);
            }
            toRecord = (fromRecord + pageSize - 1) >= totalRecord ? totalRecord    : fromRecord + pageSize - 1;
            totalPage = (totalRecord + pageSize - 1) / pageSize;
            nextPage = (currentPage==totalPage) ? 0 :currentPage + 1;
            toPage = totalPage >= (fromPage + pageNum -1) ? (fromPage + pageNum - 1) : totalPage;        if(totalPage < preSubmitPage){
                throw new FletsPrePageException("total page size " + totalPage + " is less than current page",previousPage);
            }
                    
            if(totalPage < currentPage){
                throw new FletsCurrentPageException("total page size " + totalPage + " is less than current page",previousPage);
            }
            for(int i=fromPage;i<=toPage;i++){
                pages.add(new Page(i,currentPage));
            }
            for(int i=toPage - fromPage + 2;i<= PageConstant.DEFAULT_PAGE_NUM;i++){
                pages.add(new Page(i,0));
            }
        }
    }
    (2)自定义标签:<page:header>