spring boot 引入thymleafd 的方式
时间: 2023-02-13 14:46:29 浏览: 50
在 Spring Boot 中使用 Thymeleaf 可以通过以下步骤进行配置:
1. 在项目的 pom.xml 文件中添加 Thymeleaf 的依赖:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
```
2. 在 application.properties 或者 application.yml 中进行如下配置:
```
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
```
3. 在项目中创建一个 Thymeleaf 模板文件,文件名可以自定义,但一般使用 .html 为后缀。
4. 在项目的 Controller 中使用 @Controller 注解,并在对应的方法中使用 Model 对象传递数据到模板,并使用 @RequestMapping 注解来映射请求的 URL。
5. 在模板中使用 Thymeleaf 的语法来进行数据的渲染。
例如,在模板文件中可以使用如下的语法来输出 Model 中传递的数据:
```
<p th:text="${message}">Hello, Thymeleaf!</p>
```
希望这些内容能够帮助你。如果你还有其他问题,欢迎随时给我留言。