Jpa pageable์ ์๋ชจ๋ฅด์๋ ๋ถ์ ์๋์ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํด์ฃผ์ธ์~!
2021/02/03 - [WEB/JPA] - JAP Pageable ์ฌ์ฉํ๊ธฐ
์ค์ ์ธํ ๋ถํฐ ํด๋ณผ๊ฒ์ ~!
1. build.gradle
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
2. application.yml
spring:
thymeleaf:
prefix: classpath:templates/
suffix: .html
mode: HTML5
cache: false
encoding: UTF-8
์ธํ ์๋ฃ! ํ๋ก์ ํธ ๊ตฌ์กฐ๋ ์๋์ ๊ฐ์ต๋๋ค.
3. BoardListController.java
@RequiredArgsConstructor
@Controller
public class BoardListController {
private final BoardListService boardListService;
@GetMapping("/board/list")
public String findAllBoard(Model model, Pageable pageable) {
Page<QuestionListResponse> boardListResponsePage = boardListService.findByBoard(pageable);
model.addAttribute("pages",boardListResponsePage);
model.addAttribute("maxPage",5);
return "boardList";
}
}
4. BoardListService.java
@RequiredArgsConstructor
@Service
public class BoardListService {
private final BoardRepository boardRepository;
public Page<BoardListResponse> findByBoard(Pageable pageable) {
Page<BoardEntity> boardListResponsePage = boardRepository.findAll(pageable);
return boardListResponsePage.map(
post ->
new BoardListResponse(
post.getTitle(),
post.getContents(),
post.getCreatedDate(),
post.getModifiedDate()));
}
5. BoardListResponse.java
@Builder
@Getter
@AllArgsConstructor
public class BoardListResponse {
private String title;
private String contents;
private LocalDateTime createdDate;
private LocalDateTime modifiedDate;
}
6. boardList.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Board Page</title>
</head>
<body>
<h1>Board Page</h1>
<table border="1">
<tr>
<th>๋ฑ๋ก๋ ์ง</th>
<th>์ ๋ชฉ</th>
<th>๋ด์ฉ</th>
<tr th:each="post : ${pages}">
<td th:text="${post.createdDate}">๋ฑ๋ก ๋ ์ง</td>
<td th:text="${post.title}">์ ๋ชฉ</td>
<td th:text="${post.contents}">๋ด์ฉ</td>
</tr>
</tr>
</table>
<div class="page-num"
th:with="start=${(pages.number/maxPage)*maxPage + 1},
end=(${(pages.totalPages == 0) ? 1 : (start + (maxPage - 1) < pages.totalPages ? start + (maxPage - 1) : pages.totalPages)})">
<ul>
<li th:if="${start > 1}">
<a th:href="@{/board/list?(page=0)}" th:text="'<<'"></a></li>
</li>
<li th:if="${start > 1}">
<a th:href="@{/board/list?(page=${start - maxPage})}" th:text="'<'"></a>
</li>
<li th:each="page: ${#numbers.sequence(start, end)}">
<a th:href="@{/board/list?(page=${page-1})}" th:text="${page}"></a></li>
</li>
<li th:if="${end < pages.totalPages}">
<a th:href="@{/board/list?(page=${start + maxPage})}" th:text="'>'"></a>
</li>
<li th:if="${end < pages.totalPages}">
<a th:href="@{/board/list?(page=${pages.totalPages-1})}" th:text="'>>'"></a></li>
</li>
</ul>
</div>
</body>
</html>
๋ฐ์ํ
'๐ WEB > Thymeleaf' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Thymeleaf ์ฌ์ฉํ๊ธฐ (0) | 2021.03.02 |
---|