1、在pom文件中增加thymeleaf依赖
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-thymeleaf</artifactId>
-
</dependency>
2、PageController代码(此处所用到的注解是@Controller而不是@RestController)
3、application.properties配置文件如下
-
# 定位模板的目录,spring boot此配置默认为classpath:/templates/
-
spring.mvc.view.prefix=classpath:/templates/
-
# 给返回的页面添加后缀名,spring boot默认为.html
-
spring.mvc.view.suffix=.html
-
4
-
# thymeleaf缓存设置
-
spring.thymeleaf.cache=false
-
server.tomcat.access_log_enabled=true
-
server.tomcat.basedir:target/tomcat
既然Springboot默认的模板路径为templates下,也可配置,我也尝试修改了此路径,但我的项目并没有起到任何效果,没搞懂为什么。还有一个问题,thymeleaf缓存的问题,Springboot使用thymeleaf默认是有缓存的,也就是将页面代码改了之后并不会及时刷新页面代码,你必须重新运行Springboot的Application方法才能看到页面修改后的效果。如果将thymeleaf的缓存关掉,也就是当页面代码修改后自动发布到Springboot内嵌的tomcat中去。不用再重新运行启动项目。当我使用此配置貌似也不是这样的,仍然需要重新运行项目。作为遗留问题,以后解决再更新。3