인생은 속도가 아니라 방향이다

[Thymeleaf] 타임리프 반복문 처리 및 상태변수 접근(index, count) 본문

백엔드(Back-end)/Thymeleaf

[Thymeleaf] 타임리프 반복문 처리 및 상태변수 접근(index, count)

기록하는 동구 2020. 2. 6. 18:03
반응형

안녕하세요, 기록하는 동구입니다. 오늘은 Thymeleaf 반복문처리와 반복변수들을 사용하는법을 포스팅하겠습니다.

 

첫번째로 반복문 연습이니까 리스트를 던져줘야겠죠?

Controller 에 반복문을 처리하기위해서 Product를 담은 list를 view단으로 던져줍니다.

1
2
3
4
5
6
7
8
9
10
@GetMapping("/test/each")
    public String testEach(Model model) {
        List<Product> list=new ArrayList<Product>();
        list.add(new Product("사과""10000"));
        list.add(new Product("배""20000"));
        list.add(new Product("포도""30000"));
        
        model.addAttribute("list", list);
        return "testEach";
    }
 

 

 

<table>

<tr>

<th>상품번호</th>

<th>상품이름</th>

<th>상품가격</th>

</tr>

<tr th:each="item, num : ${list}">

<td>[[${num.count}]]</td>

<td>[[${item.productName}]]</td>

<td>[[${item.productPrice}]]</td>

</tr>

<tr th:each="num, numStat: ${#numbers.sequence(1,5)}">

 

<td th:text="${'index :'+numStat.index}"></td> <!--현재 index  -->

<td th:text="${'count :'+numStat.count}"></td><!--현재 count  -->

<td th:text="${'first :'+numStat.first}"></td><!--첫번째 요소인지 return boolean  -->

<td th:text="${'last :'+numStat.last}"></td><!--마지막번째 요소인지 return boolean  -->

<td th:text="${'size :'+numStat.size}"></td><!--반복문의 크기   -->

<td th:text="${'current :'+numStat.current}"></td><!--현재 요소  -->

<td th:text="${'even : '+numStat.even}" /><!--현재반복이 짝수인  지-->

<td th:text="${'odd :'+numStat.odd}" /><!-- 현재반복이 홀수인지 -->

 

</tr>

 

</table>

 

반응형
Comments