(五)Thymeleaf标准表达式之——[7->8]条件表达式& 默认表达式

转载自:https://www.cnblogs.com/zjfjava/p/6893153.html

 

2.7 条件表达式

模板名称:condition-express.html

<1>a ? b:c  (if then:else)

<2>a?c (if else)

条件表达式(conditionthenelse)的所有三个部分都是表达式,这意味着它们可以是变量(${...}*{...}),消息(#{...}),URL(@{...})或文字('...'))。

e.g.

<li>? 'xx' :'xx'(if ? then:else)<span th:class="${title} ? 'green' :' red'">样例一</span></li>
<li>?'xx'(if ? then)<span th:class="${title1} ? 'green'">样例二</span></li>
<!--   嵌套   -->
<tr th:class="${row.even}? (${row.first}? 'first' : 'even') : 'odd'">
  ...
</tr>
<!--  Else表达式也可以省略,在这种情况下,如果条件为false,则返回null值 -->
<tr th:class="${row.even}? 'alt'">
  ...
</tr>

2.8 默认表达式(Elvis operator )

模板名称:condition-express.html

语法: ?: (if:defaultValue)

 the first one is used if it doesn’t evaluate to null, but if it does then the second one is used.(值不为null使用第一个,否则第二个)

e.g.

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

equals:

<p>Age: <span th:text="*{age != null}? *{age} : '(no age specified)'">27</span>.</p>

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值