340

Is there an if-else tag available in JSTL?

andronikus
  • 3,893
  • 1
  • 23
  • 44
Srinivasan
  • 10,548
  • 25
  • 58
  • 82
  • Possible duplicate of [if...else within JSP or JSTL](https://stackoverflow.com/questions/5935892/if-else-within-jsp-or-jstl) – BuZZ-dEE May 22 '18 at 15:32

7 Answers7

540

Yes, but it's clunky as hell, e.g.

<c:choose>
  <c:when test="${condition1}">
    ...
  </c:when>
  <c:when test="${condition2}">
    ...
  </c:when>
  <c:otherwise>
    ...
  </c:otherwise>
</c:choose>
skaffman
  • 381,978
  • 94
  • 789
  • 754
  • 5
    Aside from the wrapper tag (choose), I don't see how this is any more verbose than if/elseif/else would be. One wrapper tag hardly constitutes 'clunky as hell', no? – Steven Benitez Jan 08 '11 at 18:25
  • 21
    @Steven: It's the XML nature of it. There's more characters in the boilerplate than there is in the actual logic. – skaffman Jan 08 '11 at 18:40
  • 2
    Ah, ok. The same could be set for `` then, too. – Steven Benitez Jan 08 '11 at 19:24
  • 15
    I know I'm a bit late to the party, but `` seems a little verbose, eh? – andronikus Oct 27 '11 at 13:29
  • 7
    start nesting logic with appropriate indentation and clunky as hell will seem too kind a description. – Adam Tolley Aug 05 '13 at 15:41
  • god forbid you want to check if a HashMap is empty, then check if it contains a key, then check if that object from the keyed hash's boolean value is true or false. – EdgeCaseBerg Oct 24 '14 at 18:22
  • On the bright side it encourages developers to keep nesting to a minimum – Ryan Jan 27 '15 at 13:33
  • any alternatives in java world? – Timeless Mar 14 '15 at 02:55
  • 4
    One can still use plain old java syntax. I know I'm gonna be hated for this, but el is in no way compulsory. It was meant to be more readable and enforce separation of logic and UI, but it's too often not more readable, and the rest is about discipline. – chris Sep 23 '15 at 10:54
  • I would recommend [HAML](http://haml.info). If you use it, you'll never go back. It's beautiful. https://github.com/raymyers/JHaml – Chloe Oct 25 '18 at 22:40
106

In addition with skaffman answer, simple if-else you can use ternary operator like this

<c:set value="34" var="num"/>
<c:out value="${num % 2 eq 0 ? 'even': 'odd'}"/>
laksys
  • 2,978
  • 4
  • 23
  • 35
51

There is no if-else, just if.

<c:if test="${user.age ge 40}">
 You are over the hill.
</c:if>

Optionally you can use choose-when:

<c:choose>
  <c:when test="${a boolean expr}">
    do something
  </c:when>
  <c:when test="${another boolean expr}">
    do something else
  </c:when>
  <c:otherwise>
    do this when nothing else is true
  </c:otherwise>
</c:choose>
Menuka Ishan
  • 3,169
  • 2
  • 42
  • 55
  • Hi @iwxfer, your above link is not available right now, please update, if you can as you good score, other wise remove it. – Ajay2707 May 19 '16 at 05:51
26

I got away with simply using two if tags, thought I'd add an answer in case it's of use to anyone else:

<c:if test="${condition}">
  ...
</c:if>
<c:if test="${!condition}">
  ...
</c:if>

whilst technically not an if-else per se, the behaviour is the same and avoids the clunky approach of using the choose tag, so depending on how complex your requirement is this might be preferable.

jonk
  • 1,436
  • 9
  • 13
  • 1
    Consider the case when the condition is something complicated and ugly like ${not param.age gt 42 and someOtherVar eq 'foobar'}. You would have to store the condition into a temporary boolean variable so that you could do !condition, or write the inverse of that condition. Both ugly. The "otherwise" syntax is a guaranteed inverse. – matt burns May 11 '16 at 20:27
  • 3
    Indeed a complex condition would require either a local variable or writing the inverse, but both of those options would still work. I clarified that it would depend on how complex the requirement is as to whether this approach would be preferable over the `choose` tag. – jonk May 12 '16 at 15:56
  • 2
    I agree with this. If you have only one else, its less markeup then using c:choose – javaMoca Aug 15 '16 at 02:55
  • 1
    There's another additional benefit of having and . When the variable is null (not initialized), neither branch is executed, which is good. If you go with and , the false branch will be executed when the variable is null. – Steve Stilson May 14 '18 at 22:41
3

you have to use this code:

with <%@ taglib prefix="c" uri="http://www.springframework.org/tags/form"%>

and

<c:select>
            <option value="RCV"
                ${records[0].getDirection() == 'RCV' ? 'selected="true"' : ''}>
                <spring:message code="dropdown.Incoming" text="dropdown.Incoming" />
            </option>
            <option value="SND"
                ${records[0].getDirection() == 'SND'? 'selected="true"' : ''}>
                <spring:message code="dropdown.Outgoing" text="dropdown.Outgoing" />
            </option>
        </c:select>
ankit
  • 1,919
  • 19
  • 39
0

Besides the need to have an else, in many cases you will need to use the same condition on multiple locations.

I prefer to extract the condition into a variable:

<c:set var="conditionVar" value="#{expression}"/>

And after that, you can use the condition variable as many times as you need it:

...
<c:if test="#{conditionVar}">
       ...
</c:if>
<c:if test="#{!conditionVar}">
       ...
</c:if>
...
<c:if test="#{conditionVar}">
       ...
</c:if>
<c:if test="#{!conditionVar}">
       ...
</c:if>
...

The c:choose element is good for more complicated situations, but if you need an if else only, I think this approach is better. It is efficient and has the following benefits:

  • more readable if the variable name is well chosen
  • more reusable because the condition is extracted and the resulting variable can be reused for other ifs and in other expressions. It discourages writing the same condition (and evaluating it) multiple times.
-1

This is good and efficient approach as per time complexity prospect. Once it will get a true condition , it will not check any other after this. In multiple If , it will check each and condition.

   <c:choose>
      <c:when test="${condtion1}">
        do something condtion1
      </c:when>
      <c:when test="${condtion2}">
        do something condtion2
      </c:when>
      ......
      ......
      ...... 
      .......

      <c:when test="${condtionN}">
        do something condtionn N
      </c:when>


      <c:otherwise>
        do this w
      </c:otherwise>
    </c:choose>
Sachin Kumar
  • 804
  • 5
  • 11