cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

springELValueProvider with if else condition

Former Member
0 Likes
586

Hi All,

Please help me to write the Spring EL for if else condition, specially for nested if.

I am trying to understand the following code. @priceService.getPriceInformationsForProduct(#item).![priceValue].?[currencyIso == #currency.isocode].![value]

But not really helping me. At least explain this expression conditions. This will be very helpful for me.

Accepted Solutions (1)

Accepted Solutions (1)

arvind-kumar_avinash
Active Contributor
0 Likes

Probably the causes of your confusion are ! and ? in this expression. Please note that ! is not the logical operator in this expression; rather it has been used for Collection Projection. Please refer to the following example of Collection Projection at https://docs.spring.io/spring/docs/3.0.x/reference/expressions.html

Projection allows a collection to drive the evaluation of a sub-expression and the result is a new collection. The syntax for projection is ![projectionExpression]. Most easily understood by example, suppose we have a list of inventors but want the list of cities where they were born. Effectively we want to evaluate 'placeOfBirth.city' for every entry in the inventor list. Using projection:

 // returns [ 'Smiljan', 'Idvor' ]
 List placesOfBirth = (List)parser.parseExpression("Members.![placeOfBirth.city]");

Also, in your expression, ? has been used for Collection Selection and not as a part of the ternary operator. Please refer to the following example of Collection Selection at https://docs.spring.io/spring/docs/3.0.x/reference/expressions.html

Selection is a powerful expression language feature that allows you to transform some source collection into another by selecting from its entries.

Selection uses the syntax ?[selectionExpression]. This will filter the collection and return a new collection containing a subset of the original elements. For example, selection would allow us to easily get a list of Serbian inventors:

 List<Inventor> list = (List<Inventor>) 
       parser.parseExpression("Members.?[Nationality == 'Serbian']").getValue(societyContext);

Feel free to let me know if you still have any problem understanding the expression you have mentioned in your question and I will be happy to explain it further.

Former Member
0 Likes

Thank you very much for the explanation.

Can you please help me for the following situation: Scenario: I have 3 custom Boolean attribute(Suppose isOne,isTwo,isThree) in my Product. So, I want the value provider using springELValueProvider for 3rd attribute , which will give the result depending on the first two attribute value. OR How can I write the springELValueProvider, If I want the result of isThree depending on the isOne value. OR How can I assign value of isOne attribute directly to the isThird attribute using springELValueProvider.

This will really help me to move further.

Thank you.

Former Member
0 Likes

The attached is the 3rd scenario. I am trying to assign value of recommended to usuallyInStock usingSpringELValueProvider. But not working.

Former Member
0 Likes

Hi Arvind,

Can you please help me for the following situation:

Scenario:

Product: attributes are , - 1)nameOne of type CustomType(code(string),Name(String))

-2)nameTwo of type Boolean

Now I want to compare the string value of code using springELValueProvider.

Example:if product.getNameOne.getCode == "Compare" then nameTwo value set to true.

Please guide me on the same.

Answers (0)