1. pageModule 클래스 생성 (리스트와 보여지고싶은 페이지블럭수 필요)
package com.laptop.rfid_innotek2.util;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.laptop.rfid_innotek2.model.EventHistory;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageModule {
private int nowPage;
private int endPage;
private int startPage;
private int totalPage;
public void pageSetting(Page<?> list, int displayPage) {
this.nowPage = list.getPageable().getPageNumber()+1;
this.endPage = (int) (Math.ceil(nowPage/(double) displayPage) * displayPage);
this.startPage = (endPage - displayPage) + 1;
this.totalPage = list.getTotalPages();
if(this.endPage > this.totalPage) {
this.endPage = this.totalPage;
}
}
}
2. 페이징 처리하고싶은 API에 적용
@GetMapping("/eventHistory/search1")
public String search1(Model model, @PageableDefault(page = 0, size = 10, sort = "id", direction = Direction.DESC) Pageable pageable) {
Page<EventHistory> historyList = eventHistoryService.findAll(pageable);
PageModule pageModule = new PageModule();
int displayPage = 10;
pageModule.pageSetting(historyList, displayPage);
model.addAttribute("pageModule", pageModule);
model.addAttribute("historyList", historyList);
return "page/search1";
}
3. JSP 페이지에 적용
<div class="pageWrap">
<div class="numList">
<c:if test="${!historyList.first}"><div class='leftBtn'><a href="/eventHistory/search1?page=${historyList.number - 1}"> «</a></div></c:if>
<c:forEach var="i" begin="${pageModule.startPage}" end="${pageModule.endPage}">
<c:choose>
<c:when test="${i eq pageModule.nowPage}">
<div class="num black"><a style="color:white;" href="/eventHistory/search1?page=${i-1}">${i}</a></div>
</c:when>
<c:otherwise>
<div class="num"><a href="/eventHistory/search1?page=${i-1}">${i}</a></div>
</c:otherwise>
</c:choose>
</c:forEach>
<c:if test="${!historyList.last}"><div class='rightBtn'><a href="/eventHistory/search1?page=${historyList.number + 1}">»</a></div></c:if>
</div>
</div>
4. 결과
Pageable로 간편하게 사용하려고 만듬.
'프로그래밍 > Java' 카테고리의 다른 글
[Java] 스프링 시큐리티 OAuth2 라이브러리 네이버 사용시 UnsatisfiedDependencyException 오류 (0) | 2023.08.02 |
---|---|
[Java] 우분투 스프링부트 패키지 가이드 (0) | 2023.04.26 |
[Java] 엑셀 다운로드 기능 구현 (0) | 2022.06.27 |
[Java] List 반복문으로 add 할 때 동일한 값 출력될 때 (0) | 2022.02.08 |
[Java] javadocs SnippetException: Cannot document response fields as the response body is empty 오류 (0) | 2022.02.07 |