srpingboot 菜鸟入门 -- 6、Thymeleaf模板引擎,freemarker模板引擎

Thymeleaf是一个Java库,一个XML / XHTML / HTML5模板引擎

实现步骤

步骤一:在pom.xml中添加Themeleaf依赖

(1)Thymeleaf

srpingboot 菜鸟入门 -- 6、Thymeleaf模板引擎,freemarker模板引擎

(2)freemarker

srpingboot 菜鸟入门 -- 6、Thymeleaf模板引擎,freemarker模板引擎

步骤二:编写模板文件

在路径/src/main/resources下,创建文件夹templates,注意,名字一定要是templates,再在templates里面创建需要的模板即可。

其中Thymeleaf的模板文件是.html文件,而freemarker的模板文件是.ftl文件。

srpingboot 菜鸟入门 -- 6、Thymeleaf模板引擎,freemarker模板引擎

步骤三:写Controller类

(1)Tymeleaf

srpingboot 菜鸟入门 -- 6、Thymeleaf模板引擎,freemarker模板引擎

(2)freemarker

srpingboot 菜鸟入门 -- 6、Thymeleaf模板引擎,freemarker模板引擎其返回的是hello3.ftl。

 

最后,教大家如何关闭Themeleaf缓存(默认是打开的,建议开发过程中关闭),在application.properties文件中添加spring.thymeleaf.cache=false,下文其他配置信息:

#spring.thymeleaf.prefix=classpath:/templates/

#spring.thymeleaf.suffix=.html

#spring.thymeleaf.mode=HTML5

#spring.thymeleaf.encoding=UTF-8

# charset=<encoding> is added

#spring.thymeleaf.content-type=text/html

# set to false for hot refresh

spring.thymeleaf.cache=false