Thymeleaf表达式语法以及日期格式化
来源:互联网 发布:2015网络热词 编辑:程序博客网 时间:2024/05/16 03:38
简单表达式 (simple expressions)
${...} 变量表达式*{...} 选择变量表达式#{...} 消息表达式@{...} 链接url表达式
字面量
Text literals: 'one text' , 'Another one!' ,…文本Number literals: 0 , 34 , 3.0 , 12.3 ,…数字Boolean literals: true , false布尔Null literal: null空Literal tokens: 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
<!-- 条件判断 --><p th:text="${myText} eq 'hello my text!'?'equals':'not equals'"></p><p th:text="${myText} eq 'hello my text!!!'?'equals':'not equals'"></p>
条件表达式中的三个部分自身也可以是表达式,也可以是变量(${...}, *{...}),消息(#{...}), URL (@{...}) 或字面量 ('...')条件表达式也可以使用括号来嵌套:
<!-- 条件判断 --><p th:text="${myText} eq ${myText}?(${myText} eq 'aa' ? 'aa' : ${myText}):'not equals'"></p>
else表达式也可以省略,当条件为false时,会返回null:
<!-- 条件判断 --><p th:text="${myText} eq 'aa' ? 'aa'"></p>
(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属性的工具方法
表达式内置基本对象
#ctx : the context object.#vars: the context variables.#locale : the context locale.#request : (only in Web Contexts) the HttpServletRequest object.(在web环境下才可用)#response : (only in Web Contexts) the HttpServletResponse object.(在web环境下才可用)#session : (only in Web Contexts) the HttpSession object.(在web环境下才可用)#servletContext : (only in Web Contexts) the ServletContext object.(在web环境下才可用)
选择表达式(*{xx})
<!-- 选择表达式 --><div th:object="${custUser}"> <p th:text="*{nickname}"></p> <!-- 等同于 --> <p th:text="${custUser.nickname}"></p></div>
url连接@{…}
使用这种方式的好处就是可以自动将()内的中文参数自动进行URL编码
<!-- URL --><!-- href="/demo/page?param=%E6%B5%8B%E8%AF%95%E7%94%A8%E6%88%B7%EF%BC%81" --><a th:href="@{/demo/page(param=${custUser.nickname})}">demo page</a><!-- href="/" --><a th:href="@{/}">demo page</a><!-- href="/demo/page?param=%E6%B5%8B%E8%AF%95%E7%94%A8%E6%88%B7%EF%BC%81" --><a href="page.html" th:href="@{/demo/page(param=${custUser.nickname})}">demo page</a>
变量表达式
变量表达式可以解析OGNL语法。详尽的语法信息可以访问官网:
http://commons.apache.org/ognl/
字面值替换
<!-- 替换字面值 <p th:text="|Welcome to our application, ${custUser.nickname}!|">你好</p>等同于<p th:text="'Welcome to our application, ' + ${custUser.nickname} + '!'">你好</p>--><p th:text="|Welcome to our application, ${custUser.nickname}!|">你好</p>
数字运算
<!-- 数字运算 --><p th:text="(1+1*2/3) % 2"></p>
message表达式#{..}
从配置文件中取配置的信息
可以通过该表达式,实现国际化信息
<!-- 国际化,将submit按钮的message从messages_zh_CN.properties(默认)中获取 --><form action="page.html" th:attr="action=@{/demo/page}"> <fieldset> <input type="text" name="email" /> <input type="submit" value="Subscribe!" th:attr="value=#{demo.myMsg}"/> </fieldset></form>
属性设置
th:attr 为万能属性设置,内容为key value 形式多个属性用,号分隔
<form action="subscribe.html" th:attr="action=@{/subscribe}"> <fieldset> <input type="text" name="email" /> <input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/> </fieldset></form>
指定单个属性
<input type="submit" value="Subscribe!" th:value="#{subscribe.submit}"/><form action="subscribe.html" th:action="@{/subscribe}">
支持的属性
参见官方文档
属性介绍
<!-- 追加属性 和 前置追加 email is :12222222@xx.com12222222@xx.com is email.--><form action="page.html" th:attr="action=@{/demo/page}"> <fieldset> <input type="text" name="th:attrappend" value="email is :" th:attrappend="value='12222222@xx.com'"/> <input type="text" name="th:attrprepend" value=" is email." th:attrprepend="value='12222222@xx.com'"/> <input type="submit" value="Subscribe!" th:attr="value=#{demo.myMsg}"/> </fieldset></form><!-- 数据回显时,使用这个方法来选择 --><input type="checkbox" name="active" th:checked="${user.active}" /><select name="select"> <option >1</option> <option th:selected="true">2</option> <option>3</option></select>
循环
可以处理如下对象的遍历
java.util.Iterable
java.util.Enumeration
java.util.Iterator
java.util.Map
any array
<!-- 循环标签 --><table> <tr> <th>NAME</th> <th>PRICE</th> <th>IN STOCK</th> </tr> <tr th:each="element : ${myList}"> <td th:text="${element +' '+ elementStat.index +' '+ elementStat.count +' '+ elementStat.size +' '+ elementStat.current +' '+ elementStat.even +' '+ elementStat.odd +' '+ elementStat.first +' '+ elementStat.last}">Onions</td> </tr></table><table> <tr> <th>NAME</th> <th>PRICE</th> <th>IN STOCK</th> </tr> <tr th:each="element,iterStat : ${myList}"> <td th:text="${element +' '+ iterStat.index +' '+ iterStat.count +' '+ iterStat.size +' '+ iterStat.current +' '+ iterStat.even +' '+ iterStat.odd +' '+ iterStat.first +' '+ iterStat.last}">Onions</td> <!-- th:if 条件判断,相当于if(!(xx==xx)){} th:unless 与th:if正好相反 --> <td th:if="${not (iterStat.index==1)}">角标不为1</td> <td th:unless="${not (iterStat.index==1)}">角标为1</td> </tr></table>
循环的状态属性 默认从element+Stat取值 或者自己制定 前缀+Stat
index 角标从0开始
count 从1开始
size 集合大小
current 当前元素
even/odd 奇偶
first(boolean)
last(boolean)
switch语句
<!-- switch标签 --><div th:switch="${myText}"> <p th:case="'admin'">User is an administrator</p> <p th:case="'hello my text!'">hello my text!</p> <p th:case="*">default</p></div>
使用th:case=”*” 相当于default:
swich(x){ case 1: break; case 2: break; default:}
Fragments片段表达式~{…} 3.0以后的版本支持
1.~{templatename::selector}可以利用此功能方便地将重复的片段抽取出来如footer header
<!-- 代码片段定义在fragment.html中 --><!-- <div th:fragment="copy"> © 2011 The Good Thymes Virtual Grocery</div> --><!-- 代码片段使用其他页面均可使用该表达式引入 --><div th:insert="~{fragment :: copy}"></div><div th:insert="~{fragment :: copy}"></div><!-- 替换掉当前外层div标签 --><div th:replace="~{fragment :: copy}"></div><!-- 这么写也可以 --><div th:replace="fragment :: copy"></div>
2.~{templatename}包含指定模板中所有内容3.~{::selector}" or "~{this::selector}"自己页面内的模板可以省略掉模板名称
以上的表达式都支持在selector中支持支持的所有表达式
<div th:insert="footer :: (${user.isAdmin}? #{footer.admin} : #{footer.normaluser})"></div>
也可以不使用 th:fragment来定义片段
<div id="copy-section">© 2011 The Good Thymes Virtual Grocery</div><div th:insert="~{this :: #copy-section}"></div>
可以指定参数的Fragments片段
<!-- 指定参数的片段 --><!-- <div th:fragment="frag (onevar,twovar)"><p th:text="${onevar} + ' - ' + ${twovar}">...</p></div> --><div th:replace="::frag ('aaa','bbbb')"></div>
Fragments片段支持嵌套
<!-- 指定要嵌套的片段 --><!-- 片段嵌套 --><div th:fragment="frag2 (otherfrag)"> <div th:replace="${otherfrag}"><p>no operation</p></div></div><!-- 片段嵌套 --><div th:replace="fragment :: frag2(~{::frag('嵌套','就是嵌套')})"></div><!-- 不指定参数 --><div th:replace="fragment :: frag2(_)"></div>
这种不能放到一个页面里,否则会解析不到${otherfrag}
也可以使用表达式判断条件决定是否引入
定义局部变量
<!-- 定义局部变量 --><div th:with="firstPer='aaa'"> <p> The name of the first person is <span th:text="${firstPer}">Julius Caesar</span>. </p></div>
行内表达式
<!-- 行内表达式 --><p>今日阳光明媚,[[${custUser.nickname}]]</p><!-- 与下面的等价 --><p>今日阳光明媚,<span th:text="${custUser.nickname}"></span></p>
格式化日期等${{user.lastAccessDate}}
<td th:text="${{user.lastAccessDate}}">...</td>
使用${{xxx}}该表达式,解析器会根据符合 xxx类型–>String的转换器,进行转换
例如:
@Configurationpublic class ThymeleafConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware { .... @Override public void addFormatters(final FormatterRegistry registry) { super.addFormatters(registry); registry.addFormatter(dateFormatter()); } @Bean public DateFormatter dateFormatter() { return new MyDateFormatter(); } class MyDateFormatter extends DateFormatter{ @Override public String print(Date date, Locale locale) { return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date); } }}
- Thymeleaf表达式语法以及日期格式化
- thymeleaf中的日期格式化
- Thymeleaf 标准表达式语法
- Thymeleaf标准表达式语法
- Thymeleaf教程 (四) Thymeleaf标准表达式语法(上)
- Thymeleaf教程 (五) Thymeleaf标准表达式语法(下)
- Thymeleaf教程 (四) Thymeleaf标准表达式语法(上)
- Thymeleaf教程 (五) Thymeleaf标准表达式语法(下)
- EL表达式 格式化日期
- EL表达式格式化日期
- EL表达式格式化日期
- EL表达式格式化日期
- EL表达式格式化日期
- EL表达式格式化日期
- El表达式格式化日期
- EL表达式格式化日期
- EL表达式格式化日期时间
- el表达式数字、日期格式化
- loadrunner安装时提示“此产品的配置信息已损坏。请联系您的技术支持”的解决办法
- C++ string
- java jdbc 简单demo
- 常用的linux命令
- redis详解(三)-- 面试题
- Thymeleaf表达式语法以及日期格式化
- 【Android系列】View的事件分发机制
- 大数据&Docker&AI课程大纲
- Django blog
- android官方文档之路--Wi-Fi Peer-to-Peer
- 一个很好用的静态代码扫描工具 360FireLine
- Java设计模式
- java多线程之并行和并发
- mySql中Truncate的用法