Search code examples
springspring-el

Ternary operator in Spring


My question is similar to this question. Since that question is quite old so thought of posting new question.

I am also writing my expression in following

<property name="to" value="#{ systemProperties['BR']} == '01' ? 
    ${PROPERTY_VALUE_1_FROM_BUNDLE} : 
    ${PROPERTY_VALUE_2_FROM_BUNDLE}" />

When I fetch the value of "to" variable from my bean. Its giving me something like below

01='01'? value1 : value2

Its not parsing my expression in XML itself.

Am I doing anything wrong here?


Solution

  • You are terminating the SpEL too early; it should be...

    <property name="to" value="#{ systemProperties['BR'] == '01' ? 
        '${PROPERTY_VALUE_1_FROM_BUNDLE}' : 
        '${PROPERTY_VALUE_2_FROM_BUNDLE}' }" />
    

    Note that you also need single quotes around the placeholders so the resolved values are treated as literals.