Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF EL - replace null with default value

Tags:

null

jsf

el

Is there a way to replace null with a default value in JSF EL expressions, sort of like the Oracle NVL function?

I know I can do something like

#{obj == null ? 'None' : obj.property}

But was hoping there is an automatic way to do it so I'm not copy/pasting the same expression on both sides of the ternary Elvis operator.

I'm looking for something like

#{default(obj.property, 'None')}
like image 623
wrschneider Avatar asked Sep 11 '12 17:09

wrschneider


2 Answers

No such thing exist in EL. Not now and not in the future.

Your best bet is creating a custom EL function.

#{my:def(obj.property, 'None')}

(note: as default is a Java keyword/literal, it's invalid to use exactly this name as function name in EL)

If you happen to use JSF utility library OmniFaces, then you can use #{of:coalesce()} for the exact purpose.

See also:

  • How to create a custom EL function to invoke a static method?
like image 64
BalusC Avatar answered Nov 12 '22 10:11

BalusC


In some situations you can use this:

<c:out value="${obj.property}" default="None"/>

(as suggested here)

like image 23
David Balažic Avatar answered Nov 12 '22 10:11

David Balažic