MHLab blog
📜
thymeleaf

Thymeleaf에서 if else 조건문 사용법

2018.02.18

주의

이 문건은 과거 Hexo 블로그 (2018-02-18) 에서 이동된 문서입니다.

시간이 지남에 따라 최신 기술과 다를 수 있으니 주의 바랍니다.


thumbnail


Thymeleaf에서 조건문 사용하기

Thymeleaf를 사용하다 보면 서버측의 데이터에 대한 분기 조건이 필요할 경우가 있습니다.
이번 포스팅에서는 if와 else를 사용하는 것에 대해 알아보겠습니다.


if 문

Thymeleaf에서 if문은 다음과 같이 사용합니다.

<li th:if="${pageMaker.isPrev()}"></li>

th:if 문법으로 사용하는데 태그는 li 뿐만이 아닌 div등에 사용하여 조건이 맞는 경우 화면에 보여줄 수도 있고, 감출 수도 있습니다.
만약 다중 조건을 찾아야 하는 경우에는 다음과 같이 사용하기도 합니다.

<span th:if="${account.getAccountStatus().toString().equals('ACTIVE')}">재직중</span>
<span th:if="${account.getAccountStatus().toString().equals('VACATION')}">휴가</span>
<span th:if="${account.getAccountStatus().toString().equals('ETC')}">기타</span>

else 문

다중 if문으로도 체크 가능하지만 그 외의 값을 체크할 때 else를 사용하는데, else는 thymeleaf에서는 다른 속성인 th:unless를 사용합니다. 사용 예제 코드는 다음과 같습니다.

<td
  th:if="${dto.getFileStatus().toString()} == 'ON_SUMMER'"
  th:text="${dto.getUploadName()} + ' (에디터 첨부파일)'"
></td>
<td th:unless="${dto.getFileStatus().toString()} == 'ON_SUMMER'" th:text="${dto.getUploadName()}"></td>

위의 예제처럼 th:unless는 if문과 조건이 동일하지만 사실 if조건과 다른 조건인 경우를 검색하게 됩니다.


작은 개인광고 양해 바랍니다 ^^;;
👇 주인장이 직접 만든 서비스 👇
/static/29a05fefb322c94d5eb3f7d05c7c224e/myc_icon.png
Typescript
React
Next.Js
Nest.Js
마와셀(웹) - 와인 가격 비교
와인 가격 비교 서비스
postweb
/static/29a05fefb322c94d5eb3f7d05c7c224e/myc_icon.png
Dart
Flutter
hive
provider
마와셀(엡) - 와인과 셀러 관리, 시음노트
보유한 와인의 관리, 시음노트 작성, 보유 와인 셀러의 관리 어플리케이션
/static/d35d260fd4813f4a6d284a7f4fbcdf49/bcow_icon.png
Typescript
React
Next.Js
Nest.Js
흑우집합소(웹) - 로또번호 추천 서비스
로또번호 추천 서비스
/static/d35d260fd4813f4a6d284a7f4fbcdf49/bcow_icon.png
Dart
Flutter
drift
provider
흑우집합소(앱) - 로또번호 추천 서비스
로또번호 추천 서비스

© Powered by danmin