티스토리 뷰
<th:block> 은 HTML 태그가 아닌 타임리프의 유일한 자체 태그다.
BasicController에 추가
@GetMapping("/block")
public String block(Model model) {
addUsers(model);
return "basic/block";
}
/resources/templates/basic/block.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!-- Thymeleaf의 th:each를 사용하여 사용자 목록(users)을 반복 -->
<th:block th:each="user : ${users}">
<!-- 각 사용자에 대한 정보를 표시하는 블록 -->
<div>
<!-- 첫 번째 <div> 요소: 사용자 이름 출력 -->
사용자 이름1 <span th:text="${user.username}"></span>
<!-- 사용자 나이 출력 -->
사용자 나이1 <span th:text="${user.age}"></span>
</div>
<!-- 두 번째 <div> 요소: 사용자 정보 요약 출력 -->
<div>
<!-- 사용자 이름과 나이를 조합하여 요약 출력 -->
요약 <span th:text="${user.username} + ' / ' + ${user.age}"></span>
</div>
</th:block>
</body>
</html>
<th:block th:each="user : ${users}">
: Thymeleaf의 th:each 디렉티브를 사용하여 ${users} 변수의 사용자 목록을 반복. 각 사용자는 user 변수에 바인딩됩니다.
<span th:text="${user.username}"></span>
: Thymeleaf의 th:text 속성을 사용하여 사용자의 이름을 출력.
${user.username}은 사용자의 이름을 나타내는 표현식이다.
<span th:text="${user.age}"></span>
: 사용자의 나이를 출력하는 부분으로, ${user.age} 표현식을 사용하여 사용자의 나이를 표시.
요약
<span th:text="${user.username} + ' / ' + ${user.age}"></span>
: 사용자의 이름과 나이를 조합하여 사용자 정보의 요약을 출력.
${user.username} + ' / ' + ${user.age} 표현식은
사용자 이름과 나이를 결합하여 출력.
실행하면
<th:block> 은 렌더링시 제거된다.
728x90
반응형
'[개발] - Spring > Thymeleaf' 카테고리의 다른 글
Thymeleaf (13) 템플릿 조각 (0) | 2023.11.20 |
---|---|
Thymeleaf (12) 자바스크립트 인라인 (0) | 2023.11.18 |
Thymeleaf (10) 주석 (0) | 2023.11.16 |
Thymeleaf (9) 반복 (0) | 2023.11.15 |
Thymeleaf (8) 속성 값 설정 (0) | 2023.11.14 |
Comments