[SpringBoot] - Thymeleaf의 기본사용법(2) - 제어문 처리

ACAI BERRY DEVELOVER·2023년 6월 23일
0
post-thumbnail

Thymeleaf의 제어문 처리

  • th:if ~ unless
  • 삼항연산자

반복문과 if 처리
'sno의 값이 5의 배수들만 출력하라'

<li th:each="dto : ${list}" th:if="${dto.sno % 5 ==0}">
    [[${dto}]]
</li>
  • th:if와 th:unless
    'sno가 5로 나눈 나머지가 0인 경우에는 sno만을 출력하고, 그렇지 않다면 SampleDTO의 first를 출력하라'
<ul>
<li th:each="dto:${list}">
    <span th:if="${dto.sno % 5 == 0}" th:text="${'--------------'+dto.sno}"></span>
    <span th:unless="${dto.sno % 5 == 0}" th:text="${dto.first}"></span>
</li>
</ul>

삼항연산자
'sno가 5로 나눈 나머지가 0인 경우에는 sno를 출력하라'

<ul>
    <li th:each="dto : ${list}" th:text="${dto.sno % 5 == 0}?${dto.sno}"></li>
</ul>

'sno가 5로 나눈 나머지가 0인 경우에는 sno만을 출력하고 나머지는 first를 출력하라'

<ul>
    <li th:each="dto : ${list}" 
    th:text="${dto.sno % 5 == 0}?${dto.sno}: ${dto.first}"></li>
</ul>

특정 상황에 CSS 클래스 지정하기

<style>
    .target {
        background-color: lightpink;
    }
</style>
    <ul>
        <li th:each="dto : ${list}" th:class="${dto.sno % 5 ==0}?'target'"
        th:text="${dto}"></li>
    </ul>
profile
쓸때 대충 쓰지 말고! 공부하면서 써!

0개의 댓글