페이징 처리에 필요한 요소
1. 페이지 번호 (현제 보고 있는 페이지가 몇번째 페이지인지)
2. 한 페이지에 몇 개의 개시글을 보여줄 것인지
📑 Criteria.java (VO)
@Data
public class Criteria {
private int page; //현제 페이지 번호
private int perPageNum; //한페이지에 보여줄 게시글의 수
// 생성자 : 처음 페이지
public Criteria(){
this.page = 1;
this.perPageNum = 10;
}
// 현제 페이지의 게시글의 시작번호를 구할 수 있는 메서드 공식 - DB 에서 가져오는 index로 0부터 시작
//ex) 0~ /10~ / 20~ => MySQL limit 0, 10 // limit (시작),(갯수)
public int getPageStart (){
return (page - 1) * perPageNum;
}
}
3. 페이징 처리를 만드는 클래스
📑 PageMapker.java (VO)
@Data
public class PageMap ker {
private Critaria cri;
private int totalCount; // DB 총 게시글의 수
private int startPage; // 시작 페이지 번호 (페이징의 처음 번호)
private int endPage; // 끝 페이지 번호 (페이징의 마지막 번호) --- 일정하지 않다.
private boolean prev; // 이전 버튼 생성 (true/ false)
private boolean next; // 다음 버튼 생성 (true/ false)
private int displayPageNum = 10; // 10개씩 페이지을 보여준다.
// 총 게시글의 수가 구해지면 > 페이징 처리 값들이 구해진다.
public void setTotalCount(int totalCount){
this.totalCount = totalCount;
makePaging();
}
private void makePaging(){
// 1. 화면에 보여질 마지막 페이지 번호
endPage = Math.ceil( cri.getPage() / (double) displayPageNum ) * displayPageNum;
// 2. 화면에 보여질 시작 페이지 번호
startPage = endPage - displayPageNum + 1;
if(startPage <= 0 ) startPage = 1;
// 3. 전체 마지막 페이지 계산 / 실제 DB에서 마지막 페이지가 다를 수 있다.
int tempEndPage = (int)(Math.cell( totalCount / (double) cri.getPerPageNum() ));
// 3-1. 화면에 보여질 마지막 페이지 유효성 체크
if( tempEndPage < endPage ) {
endPage = tempEndPage;
}
// 4. 이전 페이지 버튼
if( startPage == 1 ) ? false : true;
// 5. 다음 페이지 버튼
if ( endPage < tempEndPage ) ? true : false;
}
}
1) 화면에 보여질 마지막 페이지 번호
endPage = Math.cell ( 현제 페이지 번호 / (소수점 0.0) 페이지 수) * 페이지 수
endPage = Math.ceil( cri.getPage() / (double) displayPageNum ) * displayPageNum;
2) 화면에 보여질 시작 페이지 번호
startPage = 마지막 페이지 번호 - 페이지 수 + 1;
if(startPage <= 0 ) startPage = 1; =>
startPage = endPage - displayPageNum + 1;
3) 전체 마지막 페이지 계산
int tempEndPage = (int)(Math.cell( 전체 게시글 수 / (double) 한 페이지에 보여줄 게시물 수 ));
int tempEndPage = (int)(Math.cell( totalCount / (double) cri.getPerPageNum() ));
3-1) 화면에 보여질 마지막 페이지 유효성 체크
if( 마지막 페이지(계산된) < 마지막 페이지 번호 ) { 마지막 페이지 번호 = 마지막 페이지(계산된); }
if( tempEndPage < endPage ) { endPage = tempEndPage; }
4. 이전 페이지 버튼
if( 처음 페이지 번호 == 1 ) ? false : true;
if( startPage == 1 ) ? false : true;
5. 다음 페이지 버튼
if ( 마지막 페이지 번호 < 마지막 페이지(계산된) ) ? true : false;
if ( endPage < tempEndPage ) ? true : false;
'BackEnd > Spring Boot' 카테고리의 다른 글
[ Spring ] 메시지 관리 및 국제화 적용방법 (0) | 2023.03.27 |
---|---|
[ Spring Boot ] Thymeleaf (0) | 2023.03.25 |
[ Spring Boot ] 로깅 SLF4J - @Slf4j (0) | 2023.03.23 |
[ Spring Boot ] Server 연결 (2) - 실제로 많이 사용되는 구조와 네이버 Api 연결해보기 (1) | 2023.03.13 |
[ Spring Boot ] Server 연결 (1) Get/Post- UriComponentsBuilder · RestTemplate (0) | 2023.03.12 |