Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- Spring Boot
- java
- Thymeleaf
- Proxy
- 스프링 핵심 원리
- jpa
- JDBC
- 스프링
- 스프링 핵심 기능
- Greedy
- 그리디
- kotlin
- QueryDSL
- pointcut
- Exception
- spring
- transaction
- 인프런
- 알고리즘
- 자바
- 백준
- AOP
- Servlet
- SpringBoot
- Android
- springdatajpa
- db
- JPQL
- 김영한
- http
Archives
- Today
- Total
개발자되기 프로젝트
[Thymeleaf] 반복, each 본문
1. 반복
- th:each
- 반복에서 사용할 수 있는 여러 상태 값 지원함.
2. 반복기능
- th:each
- <tr th:each="user : ${users}">
- 반복시 오른쪽 컬렉션( ${users} )의 값을 하나씩 꺼내서 왼쪽 변수( user )에 담아서 반복.
- th:each 는 List 뿐만 아니라
- 배열, java.util.Iterable , java.util.Enumeration 을 구현한 모든 객체를 반복에 사용 가능.
- Map 도 사용할 수 있음. 변수에 담기는 값은 Map.Entry
<table th:each="entry : ${map}"> <tr> <td th:text="${entry.key}">Key 1</td> <td th:text="${entry.value}">Value 1</td> </tr> </table>
3. 현재 반복 상태 알려주는 기능.
- <tr th:each="user, userStat : ${users}">
- <tr th:each="꺼낸 값, 현재 루프의 상태 : list">
- 두 번째 파라미터를 통해 반복의 상태를 확인 가능
- 두 번째 파라미터는 생략 가능한데 기본적으로 변수명+Stat
- index : 0부터 시작
- count : 1부터 시작
- size : 전체 사이즈
- even , odd : 홀수, 짝수 여부( boolean )
- first , last :처음, 마지막 여부( boolean )
- current : 현재 객체
-
<td th:text="${userStat.count}">username</td> <td th:text="${user.username}">username</td> <td th:text="${user.age}">0</td>
4. Controller
@GetMapping("/each")
public String each(Model model){
addUsers(model);
return "basic/each";
}
private void addUsers(Model model){
List<User> list = new ArrayList<>();
list.add(new User("UserA", 10));
list.add(new User("UserB", 20));
list.add(new User("UserC", 30));
model.addAttribute("users", list);
}
5. each.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>기본 테이블</h1>
<table border="1">
<tr>
<th>username</th>
<th>age</th>
</tr>
<tr th:each="user : ${users}">
<td th:text="${user.username}">username</td>
<td th:text="${user.age}">0</td>
</tr>
</table>
<h1>반복 상태 유지</h1>
<table border="1">
<tr>
<th>count</th>
<th>username</th>
<th>age</th>
<th>etc</th>
</tr>
<tr th:each="user, userStat : ${users}">
<td th:text="${userStat.count}">username</td>
<td th:text="${user.username}">username</td>
<td th:text="${user.age}">0</td>
<td>
index = <span th:text="${userStat.index}"></span>
count = <span th:text="${userStat.count}"></span>
size = <span th:text="${userStat.size}"></span>
even? = <span th:text="${userStat.even}"></span>
odd? = <span th:text="${userStat.odd}"></span>
first? = <span th:text="${userStat.first}"></span>
last? = <span th:text="${userStat.last}"></span>
current = <span th:text="${userStat.current}"></span>
</td>
</tr>
</table>
</body>
</html>
6. GitHub : 210918 Each
GitHub - bsh6463/Thymeleaf
Contribute to bsh6463/Thymeleaf development by creating an account on GitHub.
github.com
'인프런 > [인프런] 스프링 MVC 2' 카테고리의 다른 글
[Thymeleaf] 주석 (0) | 2021.09.19 |
---|---|
[Thymeleaf] 조건 부 평가, if, unless, switch, case (0) | 2021.09.19 |
[Thymeleaf] 속성 값 설정 (0) | 2021.09.18 |
[Thymeleaf] 연산 (0) | 2021.09.18 |
[Thymeleaf] Literal (0) | 2021.09.18 |
Comments