一、添加pom.xml依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
二、在application.properties中添加必要配置
注:
SpringBoot默认存放静态资源文件的目录有四个,这些目录都在类路径下:
- /META-INF/resources/
- /resources/
- /static/
- /public/
所以,假如你要指定模板路径需要加以下配置:
# 添加访问路径
spring.thymeleaf.prefix=classpath:/templates/
# 添加访问文件后缀
spring.thymeleaf.suffix=.html
# 启用模板缓存(开发时关闭缓存,不然没法看到实时页面)
spring.thymeleaf.cache=false
# Content-Type值
spring.thymeleaf.servlet.content-type=text/html
# 启用MVC Thymeleaf视图分辨率
spring.thymeleaf.enabled=true
# 模板编码
spring.thymeleaf.encoding=utf-8
# 应用于模板的模板模式
spring.thymeleaf.mode=HTML5
项目结构如图: