(五)Thymeleaf標准表達式之——[7->8]條件表達式& 默認表達式


2.7 條件表達式

模板名稱:condition-express.html
<1>a ? b:c  (if then:else)
<2>a?c (if else)
條件表達式( condition then else )的所有三個部分都是表達式,這意味着它們可以是變量( ${...} *{...} ),消息( #{...} ),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>

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM