
가. JPA 인터페이스에 findPageBy를 추가
public interface ItemRepository extends JpaRepository<Item,Long> {Add commentMore actions
Page<Item> findPageBy(Pageable page);
}
findPageBy : 원하는 페이지의 넘버와 item 개수를 설정하여 특정 페이지를 반환한다.나. 페이지 넘버 및 아이템 개수만큼 한 페이지에 상품 띄울 수 있게 구현
public Page<Item> getPage(int page_num, int item_cnt){Add commentMore actions
return itemRepository.findPageBy(PageRequest.of(page_num, item_cnt));
}
다. 서버에서 현재 페이지 번호와 전체 페이지 수를 기반으로 페이지네이션을 동적으로 생성하여 HTML에 렌더링
<div th:fragment="pagination(currentPage, totalPages)">Add commentMore actions
<nav class="pagination">
<ul class="pagination-list">
<li th:each="i : ${#numbers.sequence(1, totalPages)}">
<a th:href="@{/item/list/page/{pageNum}(pageNum=${i})}"
th:text="${i}"
th:classappend="${i == currentPage} ? 'active' : ''">
</a>
</li>
</ul>
</nav>
</div>
@GetMapping("/list/page/{num}")Add commentMore actions
String list(Model model, @PathVariable Integer num) {
Page<Item> result=itemService.getPage(num-1,5);
model.addAttribute("itemList", result);
model.addAttribute("currentPage", num);
int page_cnt=result.getTotalPages();
model.addAttribute("totalPages", result.getTotalPages());
가장 신기했던 건 다음과 같다.
<div th:each="item : ${itemList}">
Page<Item> result=itemService.getPage(num-1,5);
model.addAttribute("itemList", result);
이렇게 Page 클래스 타입 객체를 기존 html itemList로 랜더링했는데 html이 알아먹는다는 것이다.
원래 기존에는
ArrayList타입으로 줬었다.
html이라는 녀석..참.. 유도리있다.

html의 유도리를 느낄 수 있었다.
| 항목 | Env Info |
|---|---|
| 🖥️ 서버 | Tomcat |
| 🍭 프레임워크 | Spring Boot |
| 📀 데이터베이스 | MySQL with Azure |
| 📝 JPA | Hibernate |
| 🙈 외부 라이브러리 | lombok, thymeleaf, AJAX |
| 📏 디자인 패턴 | MVC |