spring cloud中如何使用Thymeleaf
时间: 2024-05-15 19:16:41 浏览: 109
使用Thymeleaf需要以下几个步骤:
1. 在pom.xml中添加如下依赖:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
```
2. 在application.properties或application.yml中添加如下配置:
```
spring.thymeleaf.cache=false
spring.thymeleaf.enabled=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML
```
3. 在controller中使用Thymeleaf:
```
@Controller
public class HomeController {
@GetMapping("/")
public String home(Model model) {
model.addAttribute("message", "Hello, World!");
return "home";
}
}
```
4. 在templates目录下创建home.html模板文件:
```
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Home</title>
</head>
<body>
<h1 th:text="${message}"></h1>
</body>
</html>
```
在浏览器中访问http://localhost:8080/,即可看到页面显示"Hello, World!"。
阅读全文
相关推荐

















