How to use if-else option in JSTL

JspJstl

Jsp Problem Overview


Is there an if-else tag available in JSTL?

Jsp Solutions


Solution 1 - Jsp

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>

Solution 2 - Jsp

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'}"/>

Solution 3 - Jsp

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>

Solution 4 - Jsp

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.

Solution 5 - Jsp

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>

Solution 6 - Jsp

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.

Solution 7 - Jsp

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSrinivasanView Question on Stackoverflow
Solution 1 - JspskaffmanView Answer on Stackoverflow
Solution 2 - JsplaksysView Answer on Stackoverflow
Solution 3 - Jspuser1418225View Answer on Stackoverflow
Solution 4 - JspjonkView Answer on Stackoverflow
Solution 5 - JspankitView Answer on Stackoverflow
Solution 6 - JspStefan MondelaersView Answer on Stackoverflow
Solution 7 - JspSachin KumarView Answer on Stackoverflow