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

Ch01. 타임리프(기본 기능) - 블록

webmaster 2022. 3. 10. 11:05
728x90
@GetMapping("/block")
public String block(Model model){
    addUsers(model);
    return "basic/block";
}
private void addUsers(Model model){
    List<User> list = new ArrayList<>();
    list.add(new User("userA", 10));
    list.add(new User("userB", 20));
    list.add(new User("userC", 30));
    model.addAttribute("users", list);
}
<!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>
  • Block으로 여러 개의 태그를 묶어서 무엇인가를 같이 적용시키고 싶을 때 block을 사용한다.
  • <th:block>은 렌더링시 제거된다.
728x90