본문 바로가기

thymeleaf

타임리프 반복문

 

 

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<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>
<h1>반복 상태 유지</h1>
<table border="1">
    <tr>
        <th>count</th>
        <th>username</th>
        <th>age</th>
        <th>etc</th>
    </tr>
    //user에는 users의 아이템이 들어가고 userStat에는 반복상태값이 들어감
    //개쩌는점: users의 아이템 변수명에 + Stat붙이는걸로 반복상태값변수 생략가능
    //즉, th:each가 있는 행의 userStat은 생략이 가능하다!
    <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>
            even? = <span th:text="${userStat.even}"></span>
            odd? = <span th:text="${userStat.odd}"></span>
            first? = <span th:text="${userStat.first}"></span>
            last? = <span th:text="${userStat.last}"></span>
            current = <span th:text="${userStat.current}"></span>
        </td>
    </tr>
</table>
</body>
</html>

'thymeleaf' 카테고리의 다른 글

타임리프 div 여러개 묶어서 반복  (0) 2022.06.27
타임리프 조건문  (0) 2022.06.27
타임리프 데이터 없을 때  (0) 2022.06.27
타임리프 리터럴 대체  (0) 2022.06.27
타임리프 href  (0) 2022.06.27