Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to print null values in EL?  RSS feed

 
David Balažic
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If the value of the property is null, I want "null" to be printed, like this:


But I get an empty string, like:


I know I can use a ternary operator, but isn't there a simpler/shorter solution? A function (like NVL() in SQL)?


Not only is this long, but the value expression is typed twice, which is a magnet for bugs.

Regards,
David
 
David Balažic
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here they suggest there is now way, except a custom function. :-(

http://stackoverflow.com/questions/12375029/jsf-el-replace-null-with-default-value
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, there's no magic. Either use the ternary operator or a custom function; I'm not seeing the problem.
 
Stefan Evans
Bartender
Posts: 1834
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose you could always use the <c:out> tag



Another pattern I use occasionally is to display a 'no records exist' message or similar
 
David Balažic
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:I suppose you could always use the <c:out> tag



That's great (and kind of obvious now...)!

Thanks!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any of the above will work and are about equally wordy. The other benefit of <c:out> is that it will escape the output if it is untrusted.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!