如何在JSTL中使用if-else選項

JSTL中是否有if-else標籤? spring


#1樓

除了skaffman答案外,簡單的if-else也能夠像這樣使用三元運算符 this

<c:set value="34" var="num"/>
<c:out value="${num % 2 eq 0 ? 'even': 'odd'}"/>

#2樓

沒有if-else,只有。 spa

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

您能夠選擇在如下狀況下使用choose-when: code

<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>

#3樓

我只使用了兩個if標籤,覺得我會添加一個答案,以防其餘人使用它: orm

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

雖然技術上不是if-else自己的行爲是相同的,避免使用的笨重的方式choose標籤,因此要根據您的要求有多複雜,這多是可取的。 pdo


#4樓

是的,可是笨拙,例如 get

<c:choose>
  <c:when test="${condition1}">
    ...
  </c:when>
  <c:when test="${condition2}">
    ...
  </c:when>
  <c:otherwise>
    ...
  </c:otherwise>
</c:choose>

#5樓

您必須使用如下代碼: it

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

form

<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>