Spring EL ternary operator (if-then-else) example

Spring EL supports ternary operator , perform “if then else” conditional checking. For example,

condition ? true : false

Spring EL in Annotation

Spring EL ternary operator with @Value annotation. In this example, if “itemBean.qtyOnHand” is less than 100, then set “customerBean.warning” to true, else set it to false.

package com.mkyong.core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("customerBean")
public class Customer { 
    @Value("#{itemBean.qtyOnHand < 100 ? true : false}") 
    private boolean warning; 
    public boolean isWarning() { 
        return warning; 
    } 
    public void setWarning(boolean warning) { 
        this.warning = warning; 
    } 

    @Override 
     public String toString() { 
         return "Customer [warning=" + warning + "]"; 
    }
}
package com.mkyong.core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("itemBean")
public class Item { 
    @Value("99") 
    private int qtyOnHand; 
    public int getQtyOnHand() { 
        return qtyOnHand; 
    } 
    public void setQtyOnHand(int qtyOnHand) { 
         this.qtyOnHand = qtyOnHand; 
    }
}

Output

Customer [warning=true]

Spring EL in XML
See equivalent version in bean definition XML file.

<beans xmlns="http://www.springframework.org/schema/beans" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xsi:schemaLocation="http://www.springframework.org/schema/beans    
                                                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
    <bean id="customerBean" class="com.mkyong.core.Customer"> 
        <property name="warning" value="#{itemBean.qtyOnHand < 100 ? true : false}" /> 
    </bean> 

    <bean id="itemBean" class="com.mkyong.core.Item"> 
        <property name="qtyOnHand" value="99" /> 
    </bean> 
</beans>

Output

Customer [warning=true]

In XML, you need to replace less than operator "<" with "<".

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,688評(píng)論 19 139
  • 菜說(shuō): 對(duì)于美食的追求是人們與生俱來(lái)的本能,我們也曾在粗糙的食物里迷失了味覺(jué),而所幸,我們并未迷失了對(duì)美味本質(zhì)的把...
    小菜不賣(mài)菜閱讀 437評(píng)論 0 0
  • 我先定義兩個(gè)模型,一個(gè)是問(wèn)題,一個(gè)是選項(xiàng),一個(gè)問(wèn)題可以有多個(gè)選項(xiàng)。一對(duì)多的類(lèi)型。 我們要查詢(xún)某個(gè)問(wèn)題下有哪些選項(xiàng)可...
    amyhy閱讀 1,546評(píng)論 0 1
  • 366環(huán)球匯跨境商城:三級(jí)營(yíng)銷(xiāo)業(yè)務(wù)詳解。 一、店鋪分銷(xiāo) 1、分銷(xiāo)商用戶(hù)層級(jí) 跨境商城分銷(xiāo),采取三級(jí)銷(xiāo)售形式,用戶(hù)可...
    366環(huán)球匯_砂礫閱讀 355評(píng)論 0 0
  • 這是一條讓人尷尬的回答,我愛(ài)過(guò)的人并沒(méi)有愛(ài)過(guò)我,在我第一次表白的時(shí)候,他就拒絕我了。但還是有一些甜蜜瞬間的,至少對(duì)...
    艾冬天閱讀 722評(píng)論 0 0

友情鏈接更多精彩內(nèi)容