Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

null check in jsf expression language

Tags:

java

jsf

el

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?

like image 538
crazyTechie Avatar asked Feb 05 '10 12:02

crazyTechie


People also ask

How do I apply a null check?

Use "==" to check a variable's value. If you set a variable to null with "=" then checking that the variable is equal to null would return true. variableName == null; You can also use "!= " to check that a value is NOT equal.


1 Answers

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')}" 
like image 60
BalusC Avatar answered Sep 19 '22 15:09

BalusC