基础语法
简单的表达式
Variable Expressions: ${…}
Selection Variable Expressions: *{…}
Message Expressions: #{…}
Link URL Expressions: @{…}
Fragment Expressions: ~{…}Text操作
String concatenation: +
Literal substitutions: |The name is ${name}|数学操作
Binary operators: +, -, *, /, %
Minus sign (unary operator): -Boolean操作
Binary operators: and, or
Boolean negation (unary operator): !, not比较操作
Comparators: >, <, >=, <= (gt, lt, ge, le)
Equality operators: ==, != (eq, ne条件操作
If-then: (if) ? (then)
If-then-else: (if) ? (then) : (else)
Default: (value) ?: (defaultvalue)在Thymeleaf中常见的对象
- ctx: the context object.
- vars: the context variables.
- locale: the context locale.
- request: (only in Web Contexts) the HttpServletRequest object.
- response: (only in Web Contexts) the HttpServletResponse object.
- session: (only in Web Contexts) the HttpSession object.
- servletContext: (only in Web Contexts) the ServletContext object.
常用的工具类
- execInfo: information about the template being processed.
- messages: methods for obtaining externalized messages inside variables expressions, in the same way as they would be obtained using #{…} syntax.
- uris: methods for escaping parts of URLs/URIs
- conversions: methods for executing the configured conversion service (if any).
- dates: methods for java.util.Date objects: formatting, component extraction, etc.
- calendars: analogous to #dates, but for java.util.Calendar objects.
- numbers: methods for formatting numeric objects.
- strings: methods for String objects: contains, startsWith, prepending/appending, etc.
- objects: methods for objects in general.
- bools: methods for boolean evaluation.
- arrays: methods for arrays.
- lists: methods for lists.
- sets: methods for sets.
- maps: methods for maps.
- aggregates: methods for creating aggregates on arrays or collections.
- ids: methods for dealing with id attributes that might be repeated (for example, as a result of an iteration).
更多的参考文档: 说明Appendix
selection object
语法形式: *{firstName}
含义: 操作在特定的对象上,而非基于整个context的对象
示例如下:
<div th:object="${session.user}">
<p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div>
除此之外, $和*是可以通用的…., Equivalent….
Link URL
语法形式: @{/url/xxx}
含义: 基于context替换为url地址
应用范围: th:href(a),
示例如下:
<a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>
相对的url
- Page-relative: user/login.html
- Context-relative: /itemdetails?id=3 (context name in server will be added automatically)
Fragments
功能: 插入子模板信息
应用标签: th:insert, th:replace
示例:
<div th:insert="~{commons :: main}">...</div>
<div th:with="frag=~{footer :: #main/text()}">
<p th:insert="${frag}">
</div>
Literal 替换
语法: | text, ${var} |
应用说明: 字符串替代操作
示例:
<span th:text="|Welcome to our application, ${user.name}!|">
等同于:
<span th:text="'Welcome to our application, ' + ${user.name} + '!'">
条件判断
示例:
<tr th:class="${row.even}? (${row.first}? 'first' : 'even') : 'odd'">
...
</tr>
<tr th:class="${row.even}? 'alt'">
... //替换了if语言的class判断
</tr>
这个替代了复杂的判断语句,非常简洁。