Showing posts with label jstl. Show all posts
Showing posts with label jstl. Show all posts

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!

Evaluate empty or null JSTL c tags

You can use the  or  for this.
 test="${empty var1}">
    var1 is empty or null.

 test="${not empty var1}">
    var1 is NOT empty or null.
or

     test="${empty var1}">
        var1 is empty or null.
    
    
        var1 is NOT empty or null.
    
The ${not empty var1} can also be done by ${!empty var1}.
To learn more about those ${} things (the Expression Language, which is a separate subject from JSTL),check here.