Thymleaf λ¬Έλ²
| νν | μ€λͺ |
|---|---|
| th:each | 컬λ μ μ κ° μμ μννμ¬ μΆλ ₯ |
| th:text | ννμ κ°μ ν μ€νΈλ‘ μΆλ ₯ |
| th:if | μ‘°κ±΄μ΄ μ°ΈμΌ λ ν΄λΉ μμ λ λλ§ |
| th:href | URL λ§ν¬λ₯Ό λμ μΌλ‘ μμ± |
| th:object | νΌ λ°μΈλ© |
| @{ β¦ } | URL λ§ν¬ ννμ |
| | β¦ | | 리ν°λ΄ λ체 |
| ${ β¦ } | λ³μ |
| *{ β¦ } | μ ν λ³μ |
| #{ β¦ } | λ©μμ§. properties κ°μ μΈλΆ μμμμ μ½λμ ν΄λΉνλ λ¬Έμμ΄ get. |
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Thymeleaf Example</title>
<link rel="stylesheet" th:href="@{/css/bootstrap.min.css}">
</head>
<body>
<h1 th:text="'Hi ' + ${user.name} + '!'">Hi User!</h1>
<p th:if="${data != null}" th:text="${data}">Placeholder for data</p>
<table>
<thead>
<tr>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr th:each="item: ${items}">
<td th:text="${item.price}">100</td>
</tr>
</tbody>
</table>
<div th:if="${data != null}">
<a th:href="@{/article/list(id=${data})}">View Articles</a>
</div>
<div>
<a th:href="@{/{itemId}/edit(itemId=${item.id})}" th:each="item: ${items}">Edit Item</a>
</div>
<table>
<thead>
<tr>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr th:object="${items}">
<td th:text="*{price}">100</td>
</tr>
</tbody>
</table>
<p th:text="#{member.register}">Register Member</p>
</body>
</html>
@Controller
public class ExampleController {
@GetMapping("/example")
public String example(Model model) {
List<Item> items = Arrays.asList(
new Item(1, "Item 1", 100),
new Item(2, "Item 2", 200)
);
model.addAttribute("items", items);
model.addAttribute("user", new User("John Doe"));
model.addAttribute("data", "Example Data");
return "example";
}
}
νμ΄μ§
- μ€νλ§μμλ νμ΄μ§μ μ²λ¦¬νκΈ° μν λ³λμ λΌμ΄λΈλ¬λ¦¬κ° μμ΄ JPAμ κ°μ²΄ μ¬μ©
- Page
- Pageable
- PageRequest
QuestionRepository.javaμ findAll λ©μλ μ μΈ
Page<Question> findAll(Pageable pageable);
QuestionService.javaμ getListλ©μλ λ³κ²½
public Page<Question> getList(int page) {
Pageable pageable = PageRequest.of(page, 10);
//page=μ‘°νν νμ΄μ§ λ²νΈ
//κ²μλ¬Ό 10κ°μ© μΆλ ₯
return this.questionRepository.findAll(pageable);
}
QuestionController.javaμ listλ©μλ λ³κ²½
@GetMapping("/list")
public String list(Model model,
@RequestParam(value="page", defaultValue="0") int page) {
<!--νμ΄μ§ νλΌλ―Έν°κ° μ λ¬λμ§ μμ κ²½μ° λν΄νΈ κ° 0-->
Page<Question> paging = this.questionService.getList(page);
model.addAttribute("paging", paging);
return "question_list";
}
| νλͺ© | μ€λͺ |
|---|---|
| paging.isEmpty | νμ΄μ§ μ‘΄μ¬ μ¬λΆ (κ²μλ¬Όμ΄ μμΌλ©΄ false, μμΌλ©΄ true) |
| paging.totalElements | μ 체 κ²μλ¬Ό κ°μ |
| paging.totalPages | μ 체 νμ΄μ§ κ°μ |
| paging.size | νμ΄μ§λΉ 보μ¬μ€ κ²μλ¬Ό κ°μ |
| paging.number | νμ¬ νμ΄μ§ λ²νΈ |
| paging.hasPrevious | μ΄μ νμ΄μ§ μ‘΄μ¬ μ¬λΆ |
| paging.hasNext | λ€μ νμ΄μ§ μ‘΄μ¬ μ¬λΆ |
question_list.htmlμ 루ν λ²μ λ³κ²½
<tr th:each="question, loop : ${paging}">
νμ΄μ§ μ΄λ 리μ€νΈ
question_list.htmlμ νμ΄μ§ 리μ€νΈ μ½λ μΆκ°
<div th:if="${!paging.isEmpty()}">
<ul class="pagination justify-content-center">
<li class="page-item" th:classappend="${!paging.hasPrevious} ? 'disabled'">
<!-- μ΄μ νμ΄μ§κ° μμΌλ©΄ λΉνμ±ν -->
<a class="page-link"
th:href="@{|?page=${paging.number-1}|}">
<!-- μ΄μ νμ΄μ§ λ§ν¬-->
<span>μ΄μ </span>
</a>
</li>
<!-- 0~(total-1)νμ΄μ§κΉμ§ 루ν-->
<li th:each="page: ${#numbers.sequence(0, paging.totalPages-1)}"
th:if="${page >= paging.number-5 and page <= paging.number+5}"
th:classappend="${page == paging.number} ? 'active'" class="page-item">
<!--νμ¬νμ΄μ§μ΄λ©΄ νμ±ν-->
<a th:text="${page}" class="page-link" th:href="@{|?page=${page}|}"></a>
</li>
<li class="page-item" th:classappend="${!paging.hasNext} ? 'disabled'">
<!-- λ€μ νμ΄μ§κ° μμΌλ©΄ λΉνμ±ν -->
<a class="page-link" th:href="@{|?page=${paging.number+1}|}">
<!-- λ€μ νμ΄μ§ λ§ν¬-->
<span>λ€μ</span>
</a>
</li>
</ul>
</div>