1.需要在pom文件中引入Thymeleaf(后面缩写成th)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
thymeleaf的模板默认路径是resources的templates
2.表达式
变量表达式${}.上下文环境中获取变量值
选择表达式*{}.指定对象中获取变量值 如果没有指定对象即和变量表达式一样
指定变量 th:object = "${user}"
<form id="userForm" th:object="${user}">
<input id="id" name="id" th:value="*{id}"/>
<input id="username" name="username" th:value="*{username}"/>
<input id="password" name="password" th:value="*{password}"/>
</form>
链接表达式@{}.拿静态资源
<script th:src="@{/webjars/jquery/jquery.js}"></script>
片段表达式~{}.
~{ viewName } 表示引入完整页面
~{ viewName ::selector} 表示在指定页面寻找片段 其中selector可为片段名、jquery选择器等
~{ ::selector} 表示在当前页寻找
首先通过th:fragment指定片段,然后th:replace路径和名字
th:replace="~{common/hello::world}"
简介写法th:replace="common/hello::world"
消息表达式#{}.用于国际化
其他表达式比如${'hello '+(user.name!=null?world:'world and you')}
本文详细介绍了如何在Spring Boot项目中配置并使用Thymeleaf模板引擎,包括依赖引入、默认路径设置及各种表达式的应用,如变量、选择、链接、片段和消息表达式。
21万+

被折叠的 条评论
为什么被折叠?



