Skip to content

Latest commit

 

History

History
82 lines (49 loc) · 1.59 KB

반복.md

File metadata and controls

82 lines (49 loc) · 1.59 KB

반복

타임리프에서 반복은 th:each 를 사용한다.


반복 기능

<tr th:each="user : ${users}">

반복시 오른쪽 컬렉션 ${users} 의 값을 하나씩 꺼내서 왼쪽 변수 user 에 담아서 태그를 반복 실행합니다.

<h1>기본 테이블</h1>
<table border="1">
    <tr>
        <th>username</th>
        <th>age</th>
    </tr>
    <tr th:each="user : ${users}">
        <td th:text="${user.username}">username</td>
        <td th:text="${user.age}">0</td>
    </tr>
</table>



반복 상태 유지

<tr th:each="user, userStat : ${users}">

user는 우리가 알듯 정상 작동하고, userStat는 현재 동작하고 있는 상태를 알려주는 것이다.



반복 상태 유지 기능

index : 0부터 시작하는 값

count : 1부터 시작하는 값

size : 전체 사이즈

even , odd : 홀수, 짝수 여부( boolean )

first , last :처음, 마지막 여부( boolean )

current : 현재 객체


이런 느낌으로 사용하는 것이다.

<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 = <span th:text="${userStat.index}"></span>
     count = <span th:text="${userStat.count}"></span>
     size = <span th:text="${userStat.size}"></span>
   </td>
</tr>	



Reference
스프링 MVC 2편 - 백엔드 웹 개발 활용 기술