Thymeleaf条件表达式和默认表达式

来源:互联网 发布:vue.js适用于什么项目 编辑:程序博客网 时间:2024/05/15 08:12

条件表达式

条件表达式condition?then:else根据conditon的值,只会有then,else中的一个表达式被求值。conditontrue->then, conditionfalse->else

<tr th:class="${row.even}? 'even' : 'odd'">  ...</tr>
其中条件表达式的三个部分`condition?then:else`本身都是表达式,所以可以是变量(`${…}`,`*{…}`), 消息(`#{…}`), URL(`@{…}`)或者是字面量(`’…’`,`123`)。通过使用括号`()`实现条件表达式嵌套
<tr th:class="${row.even}? (${row.first}? 'first' : 'even') : 'odd'">  ...</tr>
`else`部分可以被省略,此时如果`conditon`为`false`,`null`被返回
<tr th:class="${row.even}? 'alt'">  ...</tr>

默认表达式

默认表达式是条件表达式的一种变种conditon ?: else(default),没有then的部分,else部分只有在conditionnull的时候才会被求值

<div th:object="${session.user}">  ...  <p>Age: <span th:text="*{age}?: '(no age specified)'">27</span>.</p></div>
其等价于
<p>Age: <span th:text="*{age != null}? *{age} : '(no age specified)'">27</span>.</p>

默认表达式同样支持嵌套

<p>  Name:   <span th:text="*{firstName}?: (*{admin}? 'Admin' : #{default.username})">Sebastian</span></p>
0 0
原创粉丝点击