Java(基於Struts2) 分頁實現代碼_java

來源:互聯網
上載者:User

分頁實現的基本過程是這樣的:

1. 設定自己的分頁器的基本參數(可以從設定檔中讀取)

  ■每頁顯示的記錄條數

  ■每次最多顯示多少頁

2. 編寫設定分頁器其他參數的函數

    主要參數有以下幾個:

    總記錄條數

    總頁數

    當前頁號:現在顯示的頁數

    每頁顯示的記錄條數

    當前頁開始行(第一行是0行)

    第一頁頁號

    最後頁頁號

    下一頁頁號

    上一頁頁號

    畫面上顯示的起始頁號

    畫面上顯示的結束頁號

    參數基本實現原理:設定以上各個參數,實際上只需要三個參數就可以對所有的其他變數進行設定,即總記錄條數,每頁顯示記錄數,每次最多顯示多少頁。

    分頁器的代碼實現如下(省略get,set函數):

    Page.java

複製代碼 代碼如下:

{
        this.onePageSize = Integer.valueOf(PageResource.get(PageResource.ONE_PAGE_SIZE));
        this.displayPageCount = Integer.valueOf(PageResource.get(PageResource.DISPLAY_PAGE_COUNT)) - 1;
    }

    /** 頁號式導航, 最多顯示頁號數量為displayPageCount+1 */
    private int displayPageCount;

    /** 每頁顯示的記錄條數 */
    private int onePageSize;

    /** 總記錄條數 */
    private int totalRecord;

    /** 總頁數 */
    private int totalPage;

    /** 當前頁號 */
    private int currentPageNum = 1;

    /** 當前頁開始行(第一行是0行) */
    private int currentStartRow;

    /** 第一頁頁號 */
    private int firstPageNum = 1;

    /** 最後頁頁號 */
    private int lastPageNum;

    /** 下一頁頁號 */
    private int nextPageNum;

    /** 上一頁頁號 */
    private int prevPageNum;

    /** 頁號式導航 起始頁號 */
    private int startPageNum;

    /** 頁號式導航 結束頁號 */
    private int endPageNum;

    /**
     *
     * @param onePageSize
     * @param currentPageNum
     * @param totalRecord
     */
    public Page(int totalRecord) {
        this.totalRecord = totalRecord;
        this.setPageInfo();
    }

    public Page() {
    }

    public void setPageInfo() {
        this.totalPage = (totalRecord + onePageSize - 1) / onePageSize;
        this.currentPageNum = Math.max(1, Math.min(currentPageNum, totalPage));

        this.lastPageNum = this.totalPage;
        this.nextPageNum = Math.min(this.totalPage, this.currentPageNum + 1);
        this.prevPageNum = Math.max(1, this.currentPageNum - 1);

        // 分頁控制資訊
        this.currentStartRow = (this.currentPageNum - 1) * onePageSize;

        startPageNum = Math.max(this.currentPageNum - displayPageCount / 2,
                firstPageNum);
        endPageNum = Math.min(startPageNum + displayPageCount, lastPageNum);
        if (endPageNum - startPageNum < displayPageCount) {
            startPageNum = Math.max(endPageNum - displayPageCount, 1);
        }
    }


3. 編寫前端代碼(以Struts2為例)

當在前台點擊各個跳轉頁面的連結時,只需要將要跳轉到的頁號和總頁數傳給後台,後台會重新更新分頁器,進而實現頁碼的跳轉。

複製代碼 代碼如下:

<div>
            <div>
                總頁數:
                <s:property value="#request.p.totalPage" />
                總記錄數:
                <s:property value="#request.p.totalRecord" />
            </div>
            <s:url id="firstURL" action="PageAction!toPage">
                <s:param name="p.currentPageNum">
                    <s:property value="#request.p.firstPageNum" />
                </s:param>
                <s:param name="p.totalRecord">
                    <s:property value="#request.p.totalRecord" />
                </s:param>
            </s:url>
            <s:a href="%{firstURL}">首頁</s:a>

            <s:url id="prev" action="PageAction!toPage">
                <s:param name="p.currentPageNum">
                    <s:property value="#request.p.prevPageNum" />
                </s:param>
                <s:param name="p.totalRecord">
                    <s:property value="#request.p.totalRecord" />
                </s:param>
            </s:url>
            <s:a href="%{prev}">上一頁</s:a>

            <s:bean name="org.apache.struts2.util.Counter" id="counter">
                <s:param name="first" value="p.startPageNum" />
                <s:param name="last" value="p.endPageNum" />
                <s:iterator var="pageNum">
                    <s:if test="p.currentPageNum==#pageNum">
                        <s:property />
                    </s:if>
                    <s:else>
                        <s:url id="page" action="PageAction!toPage">
                            <s:param name="p.currentPageNum">
                                <s:property value="#pageNum" />
                            </s:param>
                            <s:param name="p.totalRecord">
                                <s:property value="#request.p.totalRecord" />
                            </s:param>
                        </s:url>
                        <s:a href="%{page}"><s:property /></s:a>
                    </s:else>
                </s:iterator>
            </s:bean>

            <s:url id="next" action="PageAction!toPage">
                <s:param name="p.currentPageNum">
                    <s:property value="#request.p.nextPageNum" />
                </s:param>
                <s:param name="p.totalRecord">
                    <s:property value="#request.p.totalRecord" />
                </s:param>
            </s:url>
            <s:a href="%{next}">下一頁</s:a>

         <s:url id="lastURL" action="PageAction!toPage">
                <s:param name="p.currentPageNum">
                    <s:property value="#request.p.lastPageNum" />
                </s:param>
                <s:param name="p.totalRecord">
                    <s:property value="#request.p.totalRecord" />
                </s:param>
            </s:url>
         <s:a href="%{lastURL}">尾頁</s:a>
        </div>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.