Thymeleaf表达式语法以及日期格式化
2017-09-22 16:18
387 查看
简单表达式 (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.com 12222222@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的转换器,进行转换
例如:
@Configuration public 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); } } }
相关文章推荐
- SqlServe零碎要点---sqlserver如何使用日期计算,DateTime类型的格式化以及语法格式等等。
- Thymeleaf教程 (四) Thymeleaf标准表达式语法(上)
- Sql Server 日期格式化函数及收缩数据库语法
- 验证日期的正则表达式加入闰年的判断以及思路分析
- 时间加减(可以对年;月;日;时;分;秒;周进行加减)以及日期自定义格式化
- Struts2里面 s:textfield标签 Date类型的格式化以及日期时间控件使用
- 验证日期的正则表达式加入闰年的判断以及思路分析
- [毕业生的商业软件开发之路]C#表达式以及判断语法结构
- Thymeleaf教程 (四) Thymeleaf标准表达式语法(上)
- yii在列表根据状态值显示状态名称以及日期格式化
- Angular基础教程:表达式日期格式化[转]
- js获取当前时间以及日期格式化
- javascript中根据表达式实现字符串格式化为日期——升级版
- Thymeleaf 标准表达式语法
- JavaScript日期格式化与正则表达式RegExp.$的用法
- Android 在12,24小时制,以及不同语言下日期和时间的格式化
- 时间格式化以及获取当前时间,利用日期生成随机数字
- cool-2018-01-26-关于JavaScript日期(Date)格式化原型的实际应用以及工具类
- Thymeleaf标准表达式语法
- [毕业生的商业软件开发之路]C#表达式以及判断语法结构 推荐