访问thymeleaf官网:https://www.thymeleaf.org/
Using Thymeleaf手册:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html
Thymeleaf + Spring手册:https://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html
The Thymeleaf Interactive Tutorial交互手册:http://itutorial.thymeleaf.org/
Thymeleaf Standard dialects手册:https://www.thymeleaf.org/doc/articles/standarddialect5minutes.html (推荐)
Javadocs/API:https://www.thymeleaf.org/apidocs/thymeleaf/3.0.9.RELEASE/
本文将对Thymeleaf Standard dialects手册进行学习
1.标准方言(Standard dialects)和Spring标准方言(SpringStandard dialects)
当在模板(template)中使用方言(Standard and SpringStandard)时,属性以th开头,如:<span th:text="...">
标准方言(Standard dialects)和Spring标准方言(SpringStandard dialects)相比,处理Spring标准方言包含一些集成Spring MVC 的特殊方言,其它几乎都是一样的。
2.标准表达式语法(Standard Expression syntax)
变量表达式(Variable expressions)
如果将Thymeleaf、Spring结合起来使用, OGNL表达式和Spring EL表达式均属于变量表达式。
${session.user.name}
<span th:text="${book.author.name}">
变量表达式的使用范围不仅包含变量值的输出(output),还可以应用到更加复杂的处理场景中,如:conditionals, iteration
<li th:each="book : ${books}">
选择表达式(Selection expressions)
选择表达式与变量表达式非常类似,唯一的区别是:选择表达式在预先已经选择的对象上执行,而不是在整个上下文(whole context)的Map中。
<div th:object="${book}">
...
<span th:text="*{title}">...</span>
...
</div>
消息表达式(Message (i18n) expressions)
可从一个.properties
文件中,通过key获取适合于不同地域场景的消息值。在Spring应用中,可使用MessageSource机制自动集成消息国际化。
#{main.title}
<table>
...
<th th:text="#{header.address.city}">...</th>
<th th:text="#{header.address.country}">...</th>
...
</table>
可以在消息表达式中包含变量表达式
#{${config.adminWelcomeKey}(${session.user.name})}
链接表达式(Link (URL) expressions)
例如:部署到web服务器上的一个web应用程序,上下文(context)是/myapp,则一个链接表达式可以如下:
<a th:href="@{/order/list}">...</a>
url中可以添加需要传递的参数
<a th:href="@{/order/details(id=${orderId},type=${orderType})}">...</a>
链接表达式中可以使用相对路径,也可以使用绝对路径
<a th:href="@{../documents/report}">...</a>
<a th:href="@{http://www.mycompany.com/main}">...</a>
碎片表达式Fragment expressions
使用碎片表达式,可以将项目页面中公共的部分提取出来,如header、footer部分,将其以碎片表达式的方式引入到需要使用的项目相关页面( templates)中。
最常用的碎片引用( insertion)的方式是使用th:insert
或 th:replace
<div th:insert="~{commons :: main}">...</div>
<div th:with="frag=~{footer :: #main/text()}">
<p th:insert="${frag}">
</div>
常量和操作Literals and operations
表达式预处理Expression preprocessing
表达式位于_之间,所有的_都将被作为表达式的一部分去执行。
#{selection.__${sel.code}__}
3.一些基本的属性(Some basic attributes)
下面是在标准方言中一些最基本的属性(attributes )
th:text
<p th:text="#{msg.welcome}">Welcome everyone!</p>
th:each
用于对返回的数组或集合进行循环遍历,需要一个与数组或集合中元素同类型的变量,用于指定循环过程中的当前元素
<li th:each="book : ${books}" th:text="${book.title}">En las Orillas del Sar</li>
<form th:action="@{/createOrder}">
<input type="button" th:value="#{form.submit}" />
<a th:href="@{/admin/users}">
4.JSP-->Thymeleaf
From JSP To Thymeleaf https://www.thymeleaf.org/doc/articles/petclinic.html