Tuesday, May 13, 2014

The ternary operator and JSP

This interesting bit of JSTL for JSP is worth writing about.
Background: The ternary operator allows you to combine an if-then-else statement into one line using a bunch of symbols. It makes code a bit harder to read, but it also makes it look cool.
I can turn this:


  not empty


  empty

into this:
It even parses variables for output:
And you don’t even need the c:out:
${not empty somevariable ?  somevariable: 'empty'}
Old hat yes, but I thought an interesting tidbit to write about!

No comments:

Post a Comment