Getting started with the Standard dialects in 5 minutes

文章总结
语法:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值