使用方法:首先引入依赖
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <version>2.5.12</version> </dependency> 我们可以进入到 <artifactId>spring-boot-dependencies</artifactId>
查看thymeleaf的默认版本
<thymeleaf.version>3.0.15.RELEASE</thymeleaf.version>
新版本的好处很多,静态html嵌入标签属性,浏览器可以直接打开模板文件,便于前后端联调,
但是模板必须符合xml规范
导入依赖会生成ThymeleafProperties可以看到ThymeleafProperties里边
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";
也就是说我们的html文件必须要写在templates下边,他也给我们配置过了前缀和后缀,当我们访问时只需要写html的名字就行
总结一下常用的Thymeleaf表达式
我们经常用到的两种表达式消息和变量表达式,如下图
<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>
<p>Today is: <span th:text="${today}">13 february 2011</span></p>
还有许多类似的表达式,分别用不同的符号来表示不同的表达式类型
简单表达式:
- 变量表达式:要用
${...}
- 选择变量表达式:要用
*{...}
- 消息表达式:要用
#{...}
- 链接网址表达式:要用
@{...}
- 片段表达式:要用
~{...}