如何在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>
相关文章
相关标签/搜索