Thymeleaf[블록]

조영재·2023년 6월 10일

Thymeleaf

목록 보기
12/15

타임리프의 특성상 HTML 태그안에 속성으로 기능을 정의해서 사용하지만 타임리프의 속성을 사용하기 애매한 경우 타임리프의 유일한 자체 태그 th:block 을 사용한다.

여러 요소를 그룹화해서 반복할때 사용한다.

th:block은 렌더링시 제거된다

basicController

@Controller
@RequestMapping("/basic")
public class basicController {

    @GetMapping("/block")
    public String block(Model model) {
        addUsers(model);
        return "basic/block";
    }
}

basic/block.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<th:block th:each="user : ${users}">
  <div>
    사용자 이름1 <span th:text="${user.username}"></span>
    사용자 나이1 <span th:text="${user.age}"></span>
  </div>
  <div>
    요약 <span th:text="${user.username} + ' / ' + ${user.age}"></span>
  </div>
</th:block>
</body>
</html>

실행 결과

profile
Just for fun

0개의 댓글