[Tymeleaf] 지역변수 선언 (th:with)

2024. 10. 16. 23:29JAVA 기반 웹개발/ThymeLeaf

지역변수 선언

public class User{
     private String username;
    
     public String getUsername(){
              return this.username;
     }
}
@Controller
List<User>users = new ArrayList<>();
users.add(userA);
model.attribute("users", list);
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset='utf-8'>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> Thymeleaf 테스트 </title>
</head>
<body>
  <div th:with="first=${users[0]}">
    <p><span th:text="${first.username}"></span></p>
  </div>
</body>
</html>

 

'JAVA 기반 웹개발 > ThymeLeaf' 카테고리의 다른 글

[Thymeleaf] 리터럴 (상수)  (0) 2024.10.16
[Tymeleaf] URL 링크 ( th:href="@~" )  (0) 2024.10.16
[Thymeleaf] 데이터 출력  (0) 2024.10.16
Thymeleaf 기본  (0) 2024.09.23