pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
yum
路径后面要保留 / 否则会找不到
spring:
...
thymeleaf:
mode: HTML
encoding: UTF-8
prefix: classpath:/templates/
suffix: .html
创建模板
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<h2 th:text="Hello + ${name}"> </h2>
</body>
</html>
字符串拼接优化
可以使用|围住字符串, 这样就不需要在文字后面附加’…’+’…’
<h2 th:text="|Hello ${name}|"> </h2>
Controller
@GetMapping("/login")
public String login(Model model) {
User user = new User();
user.setName("Jack");
model.addAttribute("user", user);
return "login";
}