Jaikrat
Jaikrat

Reputation: 1154

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?

Upvotes: 3

Views: 8093

Answers (2)

Jaikrat
Jaikrat

Reputation: 1154

I have resolved it using below code

ExpressionParser parser = new SpelExpressionParser();
String toMail = parser.parseExpression(to).getValue(String.class);

Had to make little here and there in XML but its responding as I wanted it to. Now I am getting either value in my "to" variable.

Upvotes: 0

Gary Russell
Gary Russell

Reputation: 174514

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.

Upvotes: 8

Related Questions