Getting started with the Standar
文章总结
语法:
${...} : Variable expressions.
*{...} : Selection expressions.
#{...} : Message (i18n) expressions.
@{...} : Link (URL) expressions.
~{...} : Fragment expressions.
例子:
2.1 Variable expressions
取 session 中的变量:
1. ${session.user.name}
2. <span th:text="${book.author.name}">
更复杂应用:
3. <li th:each="book : ${books}">
2.2 Selection expressions
与 Variable expressions 类似,
they will be executed on a previously selected object instead of the whole context variables map
示例:
1. *{customer.name}
2. 注意使用 th:object 标签
<div th:object="${book}">
...
<span th:text="*{title}">...</span>
...
</div>
2.3 Message(i18n)expressions
国际化示例:
1. #{main.title}
2. #{message.entrycreated(${entryId})}
3. 模板中使用:
<table>
...
<th th:text="#{header.address.city}">...</th>
<th th:text="#{header.address.country}">...</th>
...
</table>
4. 高级用法:在 message expressions 中使用 variable expressions
#{${config.adminWelcomeKey}(${session.user.name})}
2.4 Link(URL)expressions
原文翻译:给 url 添加上 context 和 session 信息
Link expressions are meant to build URLs and add useful context and session info to them (a process usually called URL rewriting).
示例:
So for a web application deployed at the /myapp context of your web server, an expression such as:
<a th:href="@{/order/list}">...</a>
Could be converted into something like this:
<a href="/myapp/order/list">...</a>
Or even this, if we need to keep sessions and cookies are not enabled (or the server doesn’t know yet):
<a href="/myapp/order/list;jsessionid=23fa31abd41ea093">...</a>
URLs can also take parameters:
<a th:href="@{/order/details(id=${orderId},type=${orderType})}">...</a>
Resulting in something like this:
<!-- Note ampersands (&) should be HTML-escaped in tag attributes... -->
<a href="/myapp/order/details?id=23&type=online">...</a>
Link expressions can be relative, in which case no application context will be prefixed to the URL:
<a th:href="@{../documents/report}">...</a>
Also server-relative (again, no application context to be prefixed):
<a th:href="@{~/contents/main}">...</a>
And protocol-relative (just like absolute URLs, but browser will use the same HTTP or HTTPS protocol used in the page being displayed):
<a th:href="@{//static.mycompany.com/res/initial}">...</a>
And of course, Link expressions can be absolute:
<a th:href="@{http://www.mycompany.com/main}">...</a>
But wait, in an absolute (or protocol-relative) URL… what value does the Thymeleaf Link Expression add? easy: the possibility of URL-rewriting defined by response filters: In a Servlet-based web application, for every URL being output (context-relative, relative, absolute…) Thymeleaf will always call the HttpServletResponse.encodeUrl(...) mechanism before displaying the URL. Which means that a filter can perform customized URL-rewriting for the application by means of wrapping the HttpServletResponse object (a commonly used mechanism).
2.5 Fragment expressions
最常用的两个属性:
th:insert or th:replace
示例:
1. div th:insert="~{commons :: main}">...</div>
2.
<div th:with="frag=~{footer :: #main/text()}">
<p th:insert="${frag}">
</div>
Fragment expressions can have arguments:
2.5 Expression preprocessing
格式:specified between __
示例:#{selection.${sel.code}}
说明(未懂):What we are seeing there is a variable expression (${sel.code}) that will be executed first and which result – let’s say, “ALL” – will be used as a part of the real expression to be executed afterwards, in this case an internationalization one (which would look for the message with key selection.ALL).