国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java(基于Struts2) 分頁實現代碼

2019-11-26 15:57:20
字體:
來源:轉載
供稿:網友

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

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>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 铁岭市| 定远县| 洪雅县| 施秉县| 柳河县| 祁门县| 英超| 曲麻莱县| 汶川县| 博客| 肇东市| 红原县| 荆门市| 临武县| 错那县| 潮安县| 高青县| 沁水县| 抚松县| 浏阳市| 辉县市| 昌吉市| 中牟县| 孝义市| 临沂市| 墨脱县| 枣强县| 闽清县| 扶沟县| 通榆县| 微博| 阿图什市| 英山县| 定襄县| 拉萨市| 涞源县| 壤塘县| 剑河县| 盐池县| 乌苏市| 荔波县|