Java(Struts2 기반) 페이지 분할 구현 코드

페이지 나누기 구현의 기본 과정은 다음과 같다.
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>

좋은 웹페이지 즐겨찾기