본문 바로가기

Spring

스프링mvc2편 타임리프 - 기본기능 : 조건부 평가

인프런 김영한님의 스프링 mvc2편으로 보고 정리한 글입니다.

무엇을 배웠나요?

  • 타임리프의 조건식 (if, unless → if의 반대)
  • 타임리프는 if unless에서 해당조건이 맞지 않으면 대크 자체를 헨더링 하지 않는다.
  • 만약 다음조건이 false인 경우 <span> ... </span> 부분 자체가 렌더링 되지 않고 사라진다.
  • switch → *은 만족하는 조건이 없을때 사용하는 디폴트이다.

궁금한 점은 무엇인가요?

더 필요하다고 생각한 것이 있나요?

코드

<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>

<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 border="1">
    <tr>
        <th>count</th>
        <th>username</th>
        <th>age</th>
    </tr>
    <tr>
        <td>1</td>
        <td>UserA</td>
        <td>
            <span>10</span>
            <span>미성년자</span>
            <span>미성년자</span>
        </td>
    </tr>
    <tr>
        <td>2</td>
        <td>UserB</td>
        <td>
            <span>20</span>
            
            
        </td>
    </tr>
    <tr>
        <td>3</td>
        <td>UserC</td>
        <td>
            <span>30</span>
            
            
        </td>
    </tr>
</table>

<h1>switch</h1>
<table border="1">
    <tr>
        <th>count</th>
        <th>username</th>
        <th>age</th>
    </tr>
    <tr>
        <td>1</td>
        <td>UserA</td>
        <td>
            <span>10살</span>
            
            
        </td>
    </tr>
    <tr>
        <td>2</td>
        <td>UserB</td>
        <td>
            
            <span>20살</span>
            
        </td>
    </tr>
    <tr>
        <td>3</td>
        <td>UserC</td>
        <td>
            
            
            <span>기타</span>
        </td>
    </tr>
</table>