Getting started with the Standard dialects in 5 minutes

简介: 文章总结语法:${...} : Variable expressions.*{...} : Selection expressions.#{...} : Message (i18n) expressions.

文章总结
语法:

${...} : 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).

目录
相关文章
|
1月前
|
Java
Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO resolved to 2024-11-26T20:55:26 of type java.time.format.Parsed
Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO resolved to 2024-11-26T20:55:26 of type java.time.format.Parsed
34 0
|
Java 关系型数据库 MySQL
The server time zone value ‘?й???’ is unrecognized or represents more than one time zone. You must c
报错信息如下:The server time zone value ‘?й???’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
100 0
|
7月前
|
Java 数据库连接 数据库
The server time zone value ‘Öйú±ê׼ʱ¼ä‘ is unrecognized or represents more than one time zone
The server time zone value ‘Öйú±ê׼ʱ¼ä‘ is unrecognized or represents more than one time zone
43 0
|
关系型数据库 MySQL Java
超详解The server time zone value ‘�й���׼ʱ��‘ is unrecognized or represents more than one time zone
超详解The server time zone value ‘�й���׼ʱ��‘ is unrecognized or represents more than one time zone
856 0
|
数据库连接 数据库
The server time zone value ‘‘ is unrecognized or represents more than one time zone.
The server time zone value ‘‘ is unrecognized or represents more than one time zone.
|
关系型数据库 MySQL Java
The server time zone value &#39;?й???????&#39; is unrecognized or represents more than one time zone.
The server time zone value &#39;?й???????&#39; is unrecognized or represents more than one time zone.
94 0
The server time zone value &#39;?й???????&#39; is unrecognized or represents more than one time zone.
|
关系型数据库 MySQL Java
The server time zone value ‘锟叫癸拷锟斤拷\u05FC时锟斤拷‘ is unrecognized or represents more than one time zone
The server time zone value ‘锟叫癸拷锟斤拷\u05FC时锟斤拷‘ is unrecognized or represents more than one time zone
The server time zone value ‘锟叫癸拷锟斤拷\u05FC时锟斤拷‘ is unrecognized or represents more than one time zone
|
Java 数据库连接
JDBC - The server time zone value ‘???‘ is unrecognized or represents more than one time zone
JDBC - The server time zone value ‘???‘ is unrecognized or represents more than one time zone
106 0
PAT (Advanced Level) Practice - 1014 Waiting in Line(30 分)
PAT (Advanced Level) Practice - 1014 Waiting in Line(30 分)
129 0