thymeleaf - springEL
This commit is contained in:
@@ -1,10 +1,16 @@
|
||||
package com.example.thymeleaf.basic;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/basic")
|
||||
public class BasicController {
|
||||
@@ -22,4 +28,35 @@ public class BasicController {
|
||||
|
||||
return "basic/text-unescaped";
|
||||
}
|
||||
|
||||
@GetMapping("/variable")
|
||||
public String variable(Model model) {
|
||||
User userA = new User("userA", 10);
|
||||
User userB = new User("userB", 20);
|
||||
|
||||
List<User> list = new ArrayList<>();
|
||||
list.add(userA);
|
||||
list.add(userB);
|
||||
|
||||
Map<String, User> map = new HashMap<>();
|
||||
map.put("userA", userA);
|
||||
map.put("userB", userB);
|
||||
|
||||
model.addAttribute("user", userA);
|
||||
model.addAttribute("users", list);
|
||||
model.addAttribute("userMap", map);
|
||||
|
||||
return "basic/variable";
|
||||
}
|
||||
|
||||
@Data
|
||||
static class User {
|
||||
private String username;
|
||||
private int age;
|
||||
|
||||
public User(String username, int age) {
|
||||
this.username = username;
|
||||
this.age = age;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>컨텐츠에 데이터 출력하기</h1>
|
||||
<h1>text vs utext</h1>
|
||||
<ul>
|
||||
<li>th:text = <span th:text="${data}"></span></li>
|
||||
<li>th:utext = <span th:utext="${data}"></span></li>
|
||||
|
||||
32
thymeleaf/src/main/resources/templates/basic/variable.html
Normal file
32
thymeleaf/src/main/resources/templates/basic/variable.html
Normal file
@@ -0,0 +1,32 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>SpringEL 표현식</h1>
|
||||
<ul>Object
|
||||
<li>${user.username} = <span th:text="${user.username}"></span></li>
|
||||
<li>${user['username']} = <span th:text="${user['username']}"></span></li>
|
||||
<li>${user.getUsername()} = <span th:text="${user.getUsername()}"></span></li>
|
||||
</ul>
|
||||
<ul>List
|
||||
<li>${users[0].username} = <span th:text="${users[0].username}"></span>
|
||||
</li>
|
||||
<li>${users[0]['username']} = <span th:text="${users[0]['username']}"></span>
|
||||
</li>
|
||||
<li>${users[0].getUsername()} = <span th:text="${users[0].getUsername()}"></span></li>
|
||||
</ul>
|
||||
<ul>Map
|
||||
<li>${userMap['userA'].username} = <span th:text="${userMap['userA'].username}"></span></li>
|
||||
<li>${userMap['userA']['username']} = <span th:text="${userMap['userA']['username']}"></span></li>
|
||||
<li>${userMap['userA'].getUsername()} = <span th:text="${userMap['userA'].getUsername()}"></span></li>
|
||||
</ul>
|
||||
|
||||
<h1>지역 변수 - (th:with)</h1>
|
||||
<div th:with="first=${users[0]}">
|
||||
<p>처음 사람의 이름은 <span th:text="${first.username}"></span></p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user