1、变量
-
${x}
将返回x
存储在Thymeleaf上下文中或作为请求属性的变量。 -
${param.x}
将返回一个称为的请求参数x
(可能是多值)。 -
${session.x}
将返回名为的会话属性x
。 -
${application.x}
将返回名为的Servlet上下文属性x
。2、不转义
这是th:text
属性的默认行为。如果我们希望Thymeleaf尊重我们的HTML标记而不是对其进行转义,则必须使用不同的属性:(th:utext
用于“未转义的文本”):
<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>
3、使用和显示变量
该${today}
表达式仅表示“获取今天调用的变量”,但是这些表达式可能更复杂(例如${user.name}
“获取称为用户的变量并调用其getName()
方法”)
4、标准表达式
我们已经看到了用这种语法表示的两种有效属性值:消息和变量表达式:
<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>
<p>Today is: <span th:text="${today}">13 february 2011</span></p>
-
简单表达式:
- 变量表达式:
${...}
- 选择变量表达式:
*{...}
- 消息表达:
#{...}
- 链接URL表达式:
@{...}
- 片段表达式:
~{...}
- 变量表达式:
-
文字
- 文本文字:
'one text'
,'Another one!'
,… - 号码文字:
0
,34
,3.0
,12.3
,… - 布尔文字:
true
,false
- 空文字:
null
- 文字标记:
one
,sometext
,main
,…
- 文本文字:
-
文字操作:
- 字符串串联:
+
- 文字替换:
|The name is ${name}|
- 字符串串联:
-
算术运算:
- 二元运算符:
+
,-
,*
,/
,%
- 减号(一元运算符):
-
- 二元运算符:
-
布尔运算:
- 二元运算符:
and
,or
- 布尔否定(一元运算符):
!
,not
- 二元运算符:
-
比较和平等:
- 比较:
>
,<
,>=
,<=
(gt
,lt
,ge
,le
) - 等号运算符:
==
,!=
(eq
,ne
)
- 比较:
-
条件运算符:
- 如果-则:
(if) ? (then)
- 如果-则-否则:
(if) ? (then) : (else)
- 默认:
(value) ?: (defaultvalue)
- 如果-则:
-
特殊令牌:
-
无操作:
_
所有这些功能都可以组合和嵌套:
'User is of type ' + (${user.isAdmin()} ? 'Administrator' : (${user.type} ?: 'Unknown'))
-
5、消息
如果我们的应用程序知道随时有谁在访问该站点,而我们想按名称向他们打招呼,该怎么办?
<p th:utext="#{home.welcome(${session.user.name})}">
Welcome to our grocery store, Sebastian Pepper!
</p>
消息密钥本身可以来自变量
<p th:utext="#{${welcomeMsgKey}(${session.user.name})}">
Welcome to our grocery store, Sebastian Pepper!
</p>
6、变量
获取值的其他方式
/*
* Access to properties using the point (.). Equivalent to calling property getters.
*/
${
person.father.name}
/*
* Access to properties can also be made by using brackets ([]) and writing
* the name of the property as a variable or between single quotes.
*/
${
person['father']['name']}
/*
* If the object is a map, both dot and bracket syntax will be equivalent to
* executing a call on its get(...) method.
*/
${
countriesByCode.ES}
${
personsByName['Stephen Zucchini'].age}
/*
* Indexed access to arrays or collections is also performed with brackets,
* writing the index without quotes.
*/
${
personsArray[0].name}
/*
* Methods can be called, even with arguments.
*/
${
person.createCompleteName()}
${
person.createCompleteNameWithSeparator('-')}
表达式基本对象
-
#ctx
:上下文对象。 -
#vars:
上下文变量。 -
#locale
:上下文语言环境。 -
#request
:(仅在Web上下文中)HttpServletRequest
对象。 -
#response
:(仅在Web上下文中)HttpServletResponse
对象。 -
#session
:(仅在Web上下文中)HttpSession
对象。 -
#servletContext
:(仅在Web上下文中)ServletContext
对象。表达工具对象
除了这些基本对象之外,Thymeleaf将为我们提供一组实用程序对象,这些对象将帮助我们在表达式中执行常见任务。
#execInfo
:有关正在处理的模板的信息。#messages
:用于获取变量表达式内的外部化消息的方法,与使用#{…}语法获得消息的方法相同。#uris
:用于转义部分URL / URI的方法#conversions
:用于执行已配置的转换服务(如果有)的方法。#dates
:java.util.Date
对象的方法:格式化,组件提取等。#calendars
:类似于#dates
,但用于java.util.Calendar
对象。#numbers
:格式化数字对象的方法。#strings
:String
对象的方法:包含,startsWith,前置/附加等。#objects
:一般对象的方法。#bools
:布尔值评估的方法。#arrays
:数组方法。#lists
:列表方法。#sets
:套方法。#maps
:地图方法。#aggregates
:用于在数组或集合上创建聚合的方法。#ids
:用于处理可能重复的id属性的方法(例如,由于迭代的结果)。
<p>
Today is: <span th:text="${#calendars.format(today,'dd MMMM yyyy')}">13 May 2011</span>
</p>
7、选择表达式
只要没有选定的对象,美元和星号的语法就完全一样。
什么是选定对象?使用该th:object
属性的表达式的结果。让我们在用户个人资料(userprofile.html
)页面中使用一个:
<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>
完全等同于:
<div>
<p>Name: <span th:text="${session.user.firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="${session.user.nationality}">Saturn</span>.</p>
</div>
当然,美元和星号语法可以混合使用:
<div th:object="${session.user}">
<p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div>
选择对象后,选定的对象也可以作为#object
表达式变量用于美元表达式:
<div th:object="${session.user}">
<p>Name: <span th:text="${#object.firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div>
如前所述,如果尚未执行任何对象选择,则美元和星号语法是等效的。
<div>
<p>Name: <span th:text="*{session.user.name}">Sebastian</span>.</p>
<p>Surname: <span th:text="*{session.user.surname}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{session.user.nationality}">Saturn</span>.</p>
</div>
8、链接网址
- 绝对网址:
http://www.thymeleaf.org
- 相对URL,可以是:
- 相对页面:
user/login.html
- 上下文相关:(
/itemdetails?id=3
服务器中的上下文名称将自动添加) - 相对于服务器:(
~/billing/processInvoice
允许在同一服务器中的另一个上下文(=应用程序)中调用URL。 - 相对协议网址:
//code.jquery.com/jquery-2.0.3.min.js
- 相对页面:
<!-- Will produce 'http://localhost:8080/gtvg/order/details?orderId=3' (plus rewriting) -->
<a href="details.html"
th:href="@{http://localhost:8080/gtvg/order/details(orderId=${o.id})}">view</a>
<!-- Will produce '/gtvg/order/details?orderId=3' (plus rewriting) -->
<a href="details.html" th:href="@{/order/details(orderId=${o.id})}">view</a>
<!-- Will produce '/gtvg/order/3/details' (plus rewriting) -->
<a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>
-
th:href
是修饰符属性:处理后,它将计算要使用的链接URL,并将该值设置href
为<a>
标记的属性。 -
我们被允许对URL参数使用表达式(如您在中所见
orderId=${o.id}
)。所需的URL参数编码操作也将自动执行。 -
如果需要几个参数,这些将用逗号分隔:
@{/order/process(execId=${execId},execType='FAST')}
-
URL路径中也允许使用变量模板:
@{/order/{orderId}/details(orderId=${orderId})}
-
以
/
(例如:)开头的相对URL/order/details
将自动以应用程序上下文名称作为前缀。 -
如果未启用Cookie或尚不知道,则
";jsessionid=..."
可能会将后缀添加到相对URL中,以便保留会话。这称为URL重写,Thymeleaf允许您通过使用response.encodeURL(...)
Servlet API中的机制为每个URL插入自己的重写过滤器。 -
该
th:href
属性允许我们(可选)href
在模板中具有有效的静态属性,以便当直接打开原型进行原型设计时,模板链接仍可被浏览器导航。
与消息语法(#{...}
)一样,URL基也可以是求值另一个表达式的结果:
<a th:href="@{${url}(orderId=${o.id})}">view</a>
<a th:href="@{
'/details/'+${user.login}(orderId=${o.id})}">view</a>
token
实际上,数字,布尔值和null文字是文字标记的一种特殊情况。
这些标记允许在标准表达式中进行一些简化。它们的工作方式与文本文字('...'
)完全相同,但是它们仅允许使用字母(A-Z
和a-z
),数字(0-9
),方括号([
和]
),点(.
),连字符(-
)和下划线(_
)。因此,没有空格,没有逗号等。
好的部分?令牌不需要任何引号引起来。因此,我们可以这样做:
<div th:class="content">...</div>
代替:
<div th:class="'content'"