관리 메뉴

웹개발 블로그

[Thymeleaf] 반복 본문

◆Thymeleaf(타임리프)/기초

[Thymeleaf] 반복

쿠키린 2023. 2. 7. 17:10
반복 상태 유지
<tr th:each="user, userStat : ${users}">
반복의 두번째 파라미터를 설정해서 반복의 상태를 확인 할 수 있습니다.
두번째 파라미터는 생략 가능한데, 생략하면 지정한 변수명( user ) + Stat 가 됩니다.
💥여기서는 user + Stat = userStat 이므로 생략 가능합니다.💥

# 반복 상태 유지 기능
index : 0부터 시작하는 값
count : 1부터 시작하는 값
size : 전체 사이즈
even , odd : 홀수, 짝수 여부( boolean )
first , last :처음, 마지막 여부( boolean )
current : 현재 객체

ex)

<!--첫번째 : 순수값, 두번째 : 숫자-->
<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(0부터 시작) = <span th:text="${userStat.index}"></span><br/>
        count(1부터 시작) = <span th:text="${userStat.count}"></span><br/>
        size = <span th:text="${userStat.size}"></span><br/>

        <!--홀짝여부, boolean-->
        even? = <span th:text="${userStat.even}"></span><br/>
        odd? = <span th:text="${userStat.odd}"></span><br/>

        <!--첫번째 값인지-->
        first? = <span th:text="${userStat.first}"></span><br/>
        last? = <span th:text="${userStat.last}"></span><br/>

        <!--현재 객체-->
        current = <span th:text="${userStat.current}"></span>

    </td>
</tr>