Java of based on Struts2 paging implementation code

  • 2020-04-01 02:19:32
  • OfStack

The basic process of paging implementation is as follows:

1. Set basic parameters of your own pager (can be read from the configuration file)

s number of strips per page

s maximum number of pages per display

2. Write functions that set other parameters of the pager

      The main parameters are as follows:

      Total number of records

      Total number of pages

      Current page number: the number of pages now displayed

      Number of bars per page

      Current page start line (first line is 0)

      Page number

      Last page page number

      Next page number

      Page Numbers

      The start page number shown on the screen

      The closing page number shown on the screen

      Basic implementation principle of parameters: set the above parameters, in fact, only three parameters can be set to all the other variables, that is, the total number of records, the number of records per page, how many pages at most.

      The code of the pager is as follows (omit the get and set functions) :

      Page. The Java


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

    
    private int displayPageCount;
    
    private int onePageSize;

    
    private int totalRecord;

    
    private int totalPage;

    
    private int currentPageNum = 1;
    
    private int currentStartRow;
    
    private int firstPageNum = 1;
    
    private int lastPageNum;
    
    private int nextPageNum;
    
    private int prevPageNum;
    
    private int startPageNum;
    
    private int endPageNum;
    
    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);
        //Paging control information
        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. Write front-end code (take Struts2 as an example)

When clicking the link of each jump page in the foreground, only need to jump to the page number and total pages to the background, the background will update the pager, and then realize page number jump.


<div>
            <div>
                 Total number of pages: 
                <s:property value="#request.p.totalPage" />
                 Total number of records: 
                <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}"> Home page </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}"> The previous page </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}"> The next page </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}"> back </s:a>
        </div>


Related articles: