Jak używać opcji if-else w JSTL

Czy w JSTL jest dostępny tag if-else?

 299
Author: andronikus, 2011-01-03

5 answers

Tak, ale jest niezgrabny jak cholera, np.

<c:choose>
  <c:when test="${condition1}">
    ...
  </c:when>
  <c:when test="${condition2}">
    ...
  </c:when>
  <c:otherwise>
    ...
  </c:otherwise>
</c:choose>
 481
Author: skaffman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-03 19:05:26

Dla prostego if-else możesz użyć operatora trójdzielnego w ten sposób

<c:set value="34" var="num"/>
<c:out value="${num % 2 eq 0 ? 'even': 'odd'}"/>
 95
Author: laksys,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-11-07 04:11:53

Nie ma if-else, tylko if.

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

Opcjonalnie można użyć 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>
 42
Author: Menuka Ishan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-19 06:26:31

Udało mi się po prostu użyć dwóch tagów if, pomyślałem, że dodam odpowiedź na wypadek, gdyby komuś się przydała:

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

Chociaż technicznie nie jest to if-else per se, zachowanie jest takie samo i pozwala uniknąć niezgrabnego podejścia do używania znacznika choose, więc w zależności od tego, jak skomplikowane są Twoje wymagania, może to być lepsze.

 19
Author: jonk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-19 19:16:34

Musisz użyć tego kodu:

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

I

<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>
 1
Author: ankit,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-27 13:18:23