ChatGPT解决这个技术问题 Extra ChatGPT

How to use if-else option in JSTL

Is there an if-else tag available in JSTL?

Possible duplicate of if...else within JSP or JSTL

s
skaffman

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>

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: It's the XML nature of it. There's more characters in the boilerplate than there is in the actual logic.
I know I'm a bit late to the party, but <c:otherwise> seems a little verbose, eh?
start nesting logic with appropriate indentation and clunky as hell will seem too kind a description.
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.
l
laksys

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

This is a good answer, but is highly situational for how useful it would be.
M
Menuka Ishan

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>

Hi @iwxfer, your above link is not available right now, please update, if you can as you good score, other wise remove it.
j
jonk

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.


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.
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.
I agree with this. If you have only one else, its less markeup then using c:choose
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.
a
ankit

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>

S
Stefan Mondelaers

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.


S
Sachin Kumar

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>