tags:

views:

2298

answers:

1

Hi,

Please see this Expression Language

styleClass="#{obj.validationErrorMap eq null ? ' ' :  
     obj.validationErrorMap.contains('key')?'highlight_field':'highlight_row'}"

Even if the map is null, highlight_row style is getting applied.

So I changed to

styleClass="#{empty obj.validationErrorMap ? ' ' :  
     obj.validationErrorMap.contains('key')?'highlight_field':'highlight_row'}"

Even then, highlight_row is getting applied.
if the map is empty OR null I dont want any style to be applied.

Any help? and reasons for this behaviour?

+3  A: 

Use empty (it checks both nullness and emptiness) and group the nested ternary expression by parentheses (EL is in certain implementations/versions namely somewhat problematic with nested ternary expressions). Thus, so:

styleClass="#{empty obj.validationErrorMap ? ' ' :  
 (obj.validationErrorMap.contains('key') ? 'highlight_field' : 'highlight_row')}"

If still in vain (I would then check JBoss EL configs), use the "normal" EL approach:

styleClass="#{empty obj.validationErrorMap ? ' ' :  
 (obj.validationErrorMap['key'] ne null ? 'highlight_field' : 'highlight_row')}"

Update: as per the comments, the Map turns out to actually be a List (please work on your naming conventions). To check if a List contains an item the "normal" EL way, use JSTL fn:contains (although not explicitly documented, it works for List as well).

styleClass="#{empty obj.validationErrorMap ? ' ' :  
 (fn:contains(obj.validationErrorMap, 'key') ? 'highlight_field' : 'highlight_row')}"
BalusC
Thanks. I'll Test this. validationErrorMap is not map. My incorrect naming. Sorry. Its array list which contains list of input fields that failed validation.
abhishek
You're welcome. Don't forget to mark answers which actually helpded in solving the problem to **Accepted** in half of your previous questions: http://stackoverflow.com/users/205505/abhishek (note: questions are spread over multiple pages). This will improve your *Accept Rate* which is currently a poor `50%`.
BalusC
Thanks for suggestion in the update. I wanted to use a map initially. So gave that name. But later changed to list. First expression is working fine (using parentheses). Thanks a lot
abhishek