๐ŸŒ WEB/Thymeleaf

Thymeleaf๋กœ Pageable ์ ์šฉํ•˜๊ธฐ!

์• ์ •์“ฐ 2021. 2. 5. 14:03

 

Jpa pageable์„ ์ž˜๋ชจ๋ฅด์‹œ๋Š” ๋ถ„์€ ์•„๋ž˜์˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”~!

2021/02/03 - [WEB/JPA] - JAP Pageable ์‚ฌ์šฉํ•˜๊ธฐ

 

JAP Pageable ์‚ฌ์šฉํ•˜๊ธฐ

Page๊นŒ์ง€ ์™”๋‹ค๋Š”๊ฑด... ๊ธฐ๋ณธ์ ์ธ CRUD๋ฅผ ์•Œ๊ณ  ๊ณ„์‹  ์ƒํƒœ๋กœ ์ดํ•ดํ•˜๊ณ  ์ž‘์„ฑํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค! ํ• ์ผ : ๊ฐ„๋‹จํ•œ ๋ฆฌ์ŠคํŠธ ๋ฐ์ดํ„ฐ์— Page, Sort ๊ตฌํ˜„ํ•˜์—ฌ API๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค. Service Entity์ž์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š”๊ฑด ์ ˆ๋•Œ๋กœ ํ•˜์ง€ ์•Š

aejeong.com

์„ค์ • ์„ธํŒ… ๋ถ€ํ„ฐ ํ•ด๋ณผ๊ฒŒ์š” ~! 

 

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>

 

๋ฐ˜์‘ํ˜•