Thymeleaf의 조건식에는 th:if="${...}" 와 그 반대인 th:unless="${...}" 가 있다.

th:if = "${ . . . }"
th:unless = "${ . . . }"

 

 

Controller

@GetMapping("/condition")
public String condition(Model model) {
    addUsers(model);
    return "basic/condition";
}

 

1. .HTML (if, unless)

<table border="1">
 <tr>
 <th>count</th>
 <th>username</th>
 <th>age</th>
 </tr>
 <tr th:each="user, userStat : ${users}">
 <td th:text="${userStat.count}">1</td>
 <td th:text="${user.username}">username</td>
 <td>
 <span th:text="${user.age}">0</span>
 <span th:text="'미성년자'" th:if="${user.age lt 20}"></span>
 <span th:text="'미성년자'" th:unless="${user.age ge 20}"></span>
 </td>
 </tr>
</table>

 

1. 결과 (if, unless)

if, unless

  • 타임리프에서는 주어진 조건이 부합할 시 태그를 렌더링하지 않음
  • 아래와 같이 조건의 결과가 false 인 경우 <span> . . . <span> 부분에 대한 렌더링 진행 X
<span th:text=" '미성년자' " th:if=" ${user.age lt 20} "> </span>

 

 

2. .HTML (Switch)

<table border="1">
 <tr>
 <th>count</th>
 <th>username</th>
 <th>age</th>
 </tr>
 <tr th:each="user, userStat : ${users}">
 <td th:text="${userStat.count}">1</td>
 <td th:text="${user.username}">username</td>
 <td th:switch="${user.age}">
 <span th:case="10">10살</span>
 <span th:case="20">20살</span>
 <span th:case="*">기타</span>
 </td>
 </tr>
</table>

 

 

2. 결과 (Switch)

Switch

  • * : 만족 조건이 없을 시 사용되는 기본값

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

'Thymeleaf' 카테고리의 다른 글

[Thymeleaf] 블록  (0) 2023.01.30
[Thymeleaf] 주석  (0) 2023.01.29
[Thymeleaf] 반복  (0) 2022.12.31
[Thymeleaf] 속성 값 설정  (0) 2022.12.28
[Thymeleaf] URL 링크  (0) 2022.12.27

+ Recent posts