标准语法
${…} : Variable expressions.
*{…} : Selection expressions.
{…} : Message (i18n) expressions.
@{…} : Link (URL) expressions.
~{…} : Fragment expressions.
Variable expressions
${session.user.name}
<span th:text="${book.author.name}">
((Book)context.getVariable("book")).getAuthor().getName()
<li th:each="book : ${books}">
//Here ${books} selects the variable called books from the context(环境), and evaluates(评价) it as an iterable to be used at a th:each loop.
Selection expressions
*{customer.name}
<div th:object="${book}">
...
<span th:text="*{title}">...</span>
...
</div>
{
// th:object="${book}"
final Book selection = (Book) context.getVariable("book");
// th:text="*{title}"
output(selection.getTitle());
}
Message (i18n) expressions
i18n:国际化
#{main.title}
#{message.entrycreated(${entryId})}
<table>
...
<th th:text="#{header.address.city}">...</th>
<th th:text="#{header.address.country}">...</th>
...
</table>
#{${config.adminWelcomeKey}(${session.user.name})}
Link (URL) expressions
<a th:href="@{/order/list}">...</a>
<a href="/myapp/order/list">...</a>
<a href="/myapp/order/list;jsessionid=23fa31abd41ea093">...</a>
<a th:href="@{/order/details(id=${orderId},type=${orderType})}">...</a>
<!-- Note ampersands (&) should be HTML-escaped in tag attributes... -->
<a href="/myapp/order/details?id=23&type=online">...</a>
<!--relative url-->
<a th:href="@{../documents/report}">...</a>
<a th:href="@{~/contents/main}">...</a>
<!--absolute(绝对的) URLs-->
<a th:href="@{//static.mycompany.com/res/initial}">...</a>
<a th:href="@{http://www.mycompany.com/main}">...</a>
Fragment expressions
<div th:insert="~{commons :: main}">...</div>
<div th:with="frag=~{footer :: #main/text()}">
<p th:insert="${frag}">
</div>
Literals and operations
- Literals:
Text literals: ‘one text’, ‘Another one!’,…
Number literals: 0, 34, 3.0, 12.3,…
Boolean literals: true, false
Null literal: null
Literal tokens: one, sometext, main,…
- Text operations:
String concatenation: +
Literal substitutions: |The name is ${name}|
- Arithmetic operations:
Binary operators: +, -, *, /, %
Minus sign (unary(一元的) operator): -
- Boolean operations:
Binary operators: and, or
Boolean negation(否定) (unary operator): !, not
- Comparisons and equality:
Comparators: >, <, >=, <= (gt, lt, ge, le)
Equality operators: ==, != (eq, ne)
- Conditional operators:
If-then: (if) ? (then)
If-then-else: (if) ? (then) : (else)
Default: (value) ?: (defaultvalue)
基本属性
- 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}">
- 丹丹