Springboot默认使用thymeleaf模板引擎

配置与使用
1.在application.properties文件中增加Thymeleaf模板的配置。(这一步也可以不做配置)
SpringBoot 学习系列 - 与Thymeleaf结合使用
说明一下,这些配置不是必须的,如果配置了会覆盖默认的。
在开发时建议将spring.thymeleaf.cache设置为false,否则会有缓存,导致页面没法及时看到更新后的效果。
比如你修改了一个文件,已经update到tomcat,但刷新页面还是之前的页面,就是因为缓存引起的。

2.在pom.xml中添加thymeleaf的依赖(这个步骤可以在生成的时候勾选)
SpringBoot 学习系列 - 与Thymeleaf结合使用
3.编写一个测试的Controller,用@Controller,别用@RestController

SpringBoot 学习系列 - 与Thymeleaf结合使用
可以看到,Controller与普通的SpringMVC的Controller无异。

4.编写greeting.html,如果有css和js文件,放在静态文件目录下

spring-boot项目静态文件目录:/src/java/resources/static
spring-boot项目模板文件目录:/src/java/resources/templates
所以greeting.html文件在/src/java/resources/templates下。
SpringBoot 学习系列 - 与Thymeleaf结合使用
.运行效果
SpringBoot 学习系列 - 与Thymeleaf结合使用
其它传值方式
可以通过Model传值

controller代码:
SpringBoot 学习系列 - 与Thymeleaf结合使用
index.html代码
SpringBoot 学习系列 - 与Thymeleaf结合使用
SpringBoot 学习系列 - 与Thymeleaf结合使用

相关文章: