springboot导入thymeleaf
时间: 2023-09-12 14:02:37 浏览: 148
要在Spring Boot中使用Thymeleaf,您可以按照以下步骤操作:
1. 在pom.xml文件中添加以下依赖项:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
```
2. 在application.properties文件中添加以下配置:
```properties
spring.thymeleaf.enabled=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
```
3. 创建一个HTML模板文件,例如index.html,放在src/main/resources/templates目录下。
4. 在Spring Boot的控制器方法中返回模板名称,例如:
```java
@GetMapping("/")
public String index() {
return "index";
}
```
这将返回名为“index”的模板文件,它位于src/main/resources/templates目录下。
5. 在模板文件中使用Thymeleaf语法来处理动态内容,例如:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Spring Boot Thymeleaf Demo</title>
</head>
<body>
<h1 th:text="${message}">Hello World!</h1>
</body>
</html>
```
这将在页面中显示一个标题,内容为从控制器方法中传递的名为“message”的模型属性。
以上就是在Spring Boot中使用Thymeleaf的基本步骤。
阅读全文
相关推荐














