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>
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.