thymeleaf 基本语法
四、标准表达式语法
· 简单表达式 (simple expressions)
${...} 变量表达式
*{...} 选择变量表达式
#{...} 消息表达式
@{...} 链接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
(if) ? (then) : (else) if-then-else
<tr th:class="${row.even}? 'even' : 'odd'">
...
</tr>
条件表达式中的三个部分自身也可以是表达式,也可以是变量(${...}, *{...}), 消息(#{...}), URL (@{...}) 或字面量 ('...')
条件表达式也可以使用括号来嵌套:
<tr th:class="${row.even}? (${row.first}? 'first' : 'even') : 'odd'">
...
</tr>
else表达式也可以省略,当条件为false时,会返回null:
<tr th:class="${row.even}? 'alt'">
...
</tr>
(value) ?: (defaultvalue) Default
只有在第一个表达式返回null时,第二个表达式才会运算
·表达式工具对象
- #dates 与java.util.Date对象的方法对应,格式化、日期组件抽取等等
- #calendars 类似#dates,与java.util.Calendar对象对应
- #numbers 格式化数字对象的工具方法
- #strings 与java.lang.String对应的工具方法:contains、startsWith、prepending/appending等等
- #objects 用于对象的工具方法
- #bools 用于布尔运算的工具方法
- #arrays 用于数组的工具方法
- #lists 用于列表的工具方法
- #sets 用于set的工具方法
- #maps 用于map的工具方法
- #aggregates 用于创建数组或集合的聚合的工具方法
- #messages 用于在变量表达式内部获取外化消息的工具方法,与#{…}语法获取的方式相同
- #ids 用于处理可能重复出现(例如,作为遍历的结果)的id属性的工具方法
·链接URL
URL在web模板中是一级重要元素,使用@{…}表示
URL的类型:
绝对URL:
- http://www.thymeleaf.org
相对URL:
- 页面相对: user/login.html
- 上下文相对:/itemdetails?id=3 (服务器上下文名称会被自动添加)
- 服务器相对:~/billing/processInvoice(允许调用同一服务器上的另一个上下文中的URL)
- 协议相对://code.jquery.com/jquery-2.0.3.min.js
Thymeleaf在任何情况下都可以处理绝对URL,对于相对URL,则需要使用一个实现了IWebContext接口的上下文对象,这个对象包含了来自HTTP请求的信息,这些信息用于创建相对链接。
<!-- 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>
· 预处理
Thymeleaf提供预处理表达式的功能。
它是在表壳式正常执行前执行的操作,允许修改最终将要被执行的表达式。
预处理表达式跟正常的一样,但被两个下划线包围住,例如:__${expression}__
假设有一个i18n消息文件Message_fr.properties,里面有一个条目包含了一个调用具体语言的静态方法的OGNL表达式:
article.text=@myapp.translator.Translator@translateToFrench({0})
Messages_es.properties中的等价条目:
article.text=@myapp.translator.Translator@translateToSpanish({0})
可以根据locale先创建用于运算表达式的标记片段,本例中,先通过预处理选择表达式,然后让Thymeleaf处理这个选择出来的表达式:
<p th:text="${__#{article.text('textVar')}__}">Some text here...</p>
对于locale为French的情况,上面的表达式经过预处理后,得出的等价物如下:
<p th:text="${@myapp.translator.Translator@translateToFrench(textVar)}">Some text here...</p>
五、 设置属性值
th:attr 任何属性值
<form action="subscribe.html" th:attr="action=@{/subscribe}">
<fieldset>
<input type="text" name="email" />
<input type="submit" value="Subscribe me!" th:attr="value=#{subscribe.submit}"/>
</fieldset>
</form>
多个属性一起设置,用逗号隔开
<img src="../../images/gtvglogo.png" th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" />
设置指定属性
th:abbr th:accept th:accept-charset
th:accesskey th:action th:align
th:alt th:archive th:audio
th:autocomplete th:axis th:background
th:bgcolor th:border th:cellpadding
th:cellspacing th:challenge th:charset
th:cite th:class th:classid ...
<input type="submit" value="Subscribe me!" th:value="#{subscribe.submit}"/>
<form action="subscribe.html" th:action="@{/subscribe}">
<li><a href="product/list.html" th:href="@{/product/list}">Product List</a></li>
设置多个属性在同一时间 有两个特殊的属性可以这样设置: th:alt-title 和 th:lang-xmllang
th:alt-title 设置 alt 和 title
th:lang-xmllang 设置 lang 和 xml:lang
<img src="../../images/gtvglogo.png" th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" /> <img src="../../images/gtvglogo.png"th:src="@{/images/gtvglogo.png}" th:title="#{logo}" th:alt="#{logo}" /> <img src="../../images/gtvglogo.png"th:src="@{/images/gtvglogo.png}" th:alt-title="#{logo}" />
前置和后置添加属性值 th:attrappend 和 th:attrprepend
<input type="button" value="Do it!" class="btn" th:attrappend="class=${' ' + cssStyle}" />
编译后:
<input type="button" value="Do it!" class="btn warning" />
还有两个特定的添加属性 th:classappend 和 th:styleappend
<tr th:each="prod : ${prods}" class="row" th:classappend="${prodStat.odd}? 'odd'">
修复的布尔属性
<input type="checkbox" name="active" th:checked="${user.active}" />
所有修复的布尔属性:
|th:async |th:autofocus |th:autoplay | |th:checked |th:controls |th:declare | |th:default |th:defer |th:disabled | |th:formnovalidate|th:hidden |th:ismap | |th:loop |th:multiple |th:novalidate | |th:nowrap |th:open |th:pubdate | |th:readonly |th:required |th:reversed | |th:scoped |th:seamless |th:selected |
HTML5友好的属性及元素名
<table>
<tr data-th-each="user : ${users}">
<td data-th-text="${user.login}">...</td>
<td data-th-text="${user.name}">...</td>
</tr>
</table>
data-{prefix}-{name}是编写HTML5自定义属性的标准语法,不需要开发者使用th:*这样的命名空间,Thymeleaf让这种语法自动对所有dialect都可用。
六、遍历
·基础
<tr th:each="prod : ${prods}">
<td th:text="${prod.name}">Onions</td>
<td th:text="${prod.price}">2.41</td>
<td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>
可遍历的对象:实现java.util.Iterable、java.util.Map(遍历时取java.util.Map.Entry)、array、任何对象都被当作只有对象自身一个元素的列表
·状态
- 当前遍历索引,从0开始,index属性
- 当前遍历索引,从1开始,count属性
- 总元素数量,size属性
- 每一次遍历的iter变量,current属性
- 当前遍历是even还是odd,even/odd布尔属性
- 当前遍历是第一个,first布尔属性
- 当前遍历是最后一个,last布尔属性
<tr th:each="prod,iterStat : ${prods}" th:class="${iterStat.odd}? 'odd'">
<td th:text="${prod.name}">Onions</td>
<td th:text="${prod.price}">2.41</td>
<td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>
若不指定状态变量,Thymeleaf会默认生成一个名为“变量名Stat”的状态变量:
<tr th:each="prod : ${prods}" th:class="${prodStat.odd}? 'odd'">
<td th:text="${prod.name}">Onions</td>
<td th:text="${prod.price}">2.41</td>
<td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>
七、条件运算
<tr th:each="prod : ${prods}" th:class="${prodStat.odd}? 'odd'">
<td th:text="${prod.name}">Onions</td>
<td th:text="${prod.price}">2.41</td>
<td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
<td>
<span th:text="${#lists.size(prod.comments)}">2</span> comment/s
<a href="comments.html" th:href="@{/product/comments(prodId=${prod.id})}" th:if="${not #lists.isEmpty(prod.comments)}">view</a>
</td>
</tr>
<a href="comments.html" th:href="@{/product/comments(prodId=${prod.id})}" th:if="${not #lists.isEmpty(prod.comments)}">view</a>
th:if 不只运算布尔条件,它对以下情况也运算为true:
·值不为null
值为boolean且为true
值为数字且非0
值为字符且非0
值是字符串且不是:“false”,“off”,“no”
值不是boolean、数字、字符、字符串
·如果值为null,则th:if运算结果为false
th:if的反面是th:unless
<a href="comments.html" th:href="@{/comments(prodId=${prod.id})}" th:unless="${#lists.isEmpty(prod.comments)}">view</a>
th:switch 和 th:case
<div th:switch="${user.role}">
<p th:case="'admin'">User is an administrator</p>
<p th:case="#{roles.manager}">User is a manager</p>
</div> <div th:switch="${user.role}">
<p th:case="'admin'">User is an administrator</p>
<p th:case="#{roles.manager}">User is a manager</p>
<p th:case="*">User is some other thing</p>
</div>
thymeleaf 基本语法的更多相关文章
- thymeleaf 添加语法提示
thymeleaf 添加语法提示: xmlns:th="http://www.thymeleaf.org"
- Thymeleaf常用语法:表达式语法之运算符
Thymeleaf表达式语法之常量分为字符串常量.数字常量.布尔值常量.空值常量:运算符分为算术运算符.关系运算符.条件运算符.无操作符. 开发环境:IntelliJ IDEA 2019.2.2Spr ...
- Thymeleaf常用语法:模板片断
系统中的很多页面有很多公共内容,例如菜单.页脚等,这些公共内容可以提取放在一个称为“模板片断”的公共页面里面,其它页面可以引用这个 “模板片断”内容. 一.模板片断的定义 可以是html标签,也可以使 ...
- Thymeleaf常用语法:模板注释
Thymeleaf模板注释分为标准HTML/XML注释.解析层注释.原型注释三种. 一.注释说明 1.标准HTML/XML注释 直接通过浏览器打开,不显示,Thymeleaf模板引擎解析也不处理,但查 ...
- 1-9springboot之thymeleaf常用语法(html页面)
一.引用命名空间 <html xmlns:th="http://www.thymeleaf.org"> 在html中引入此命名空间,可避免编辑器出现html验证错误,虽 ...
- thymeleaf常用语法
常用标签语法:①th:text<span th:text="${name}">1</span>注释:如果${name}有值则将替换掉1的值,若无则为1 ②t ...
- Thymeleaf常用语法:使用星号表达式
在处理模板时,一般情况都是使用变量表达式 ${...} 来显示变量,还可以使用选定对象表达式 *{...},它也称为星号表达式.如果在模板中先选定了对象,则需要使用星号表达式.Thymeleaf的内置 ...
- Thymeleaf常用语法:数据延迟加载
在处理模板时,可以由模板逻辑决定是否加载数据,以提高性能.在Spring Boot控制器中设置数据时,使用LazyContextVariable可以实现这功能. 开发环境:IntelliJ IDEA ...
- Thymeleaf常用语法:数据迭代
Thymeleaf数据迭代使用th:each属性,可以迭代数组.List.Set和Map等,数组.List.Set的迭代方法类似,迭代Map则会得到一个java.util.Map.Entry对象.在迭 ...
随机推荐
- node.js在windows下的学习笔记(9)---文件I/O模块
开发中我们经常会有文件I/O的需求,node.js中提供一个名为fs的模块来支持I/O操作,fs模块的文件I/O是对标准POSIX函数的简单封装. 1.将"hello world" ...
- ios开发——实用技术篇Swift&Swift调用C、C++、Object
Swift调用C.C++.Object 1.Swift调用C语言a,首先在项目中添加 CFile 文件命名为CHello,同时产生桥梁文件. b,创建之后的项目结构 b,在CHello.h文件中编写接 ...
- Windows二进制文件的Python扩展包
http://www.lfd.uci.edu/~gohlke/pythonlibs/ https://pypi.python.org/simple/
- Android实现资料收藏
1,调web浏览器 Uri myBlogUri = Uri.parse("http://xxxxx.com"); returnIt = new Intent(Intent.ACTI ...
- ListView滑动不爽,滚动一页得滑几次?该用分页列表啦!
ListView等滚动位置经常不符合用户期望: 很多时候都是看完一页想滑到下一页,但滑动一次距离往往不是不够就是超过,很难控制. PagedListView工程中提供了PageScroller来解决这 ...
- URL格式编码与解码
char* urlencode(const void* buf, size_t size) { _assert_(buf && size <= MEMMAXSIZ); const ...
- wireshark的ubuntu更新ppa源
默认的ppa源安装的是1.8.3的,这个源直接更新到1.11.0 $ sudo add-apt-repository ppa:dreibh/ppa $ sudo apt-get update $ su ...
- VIM 选择多行,复制粘贴
进入VIM,比如编辑一个文件, 1.进行选择,是V模式,按V键,进入该模式,然后选择要复制的行 2. 选择好之后,再按y键,即使复制到了 3.然后光标进入要复制的行之后,按一下P键,就粘贴了,oh y ...
- Android——获取网络图片
布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:too ...
- $(document).ready() 、 $('#id').load() 、window.onload 的区别
今天做项目的时候遇到一个问题,结果死在了$(document).ready(). $('#id').load() .window.onload的区别上.然后,就整理一下,这三者的区别. 参考文章:ht ...