<c:when> 是否像 `else if` 或 `if` 一样?
Does <c:when> act like `else if` or `if`?
使用 jstl 核心标签库,我有多个条件要使用 <c:choose>
.
检查
<c:choose>
<c:when test="${obj1.enabled}">
${obj1.name}
</c:when>
<c:when test="${obj2.enabled}">
${obj2.name}
</c:when>
<c:otherwise>
<spring:message code="error.NoRecordFound"/>
</c:otherwise>
<c:choose>
我读为 "if object 1 is enabled, print its name, else if object 2 is enabled, print its name, else print error message"。
但是 obj1
和 obj2
都可以启用,我担心它实际上会表现得像 "If object 1 is enabled, print its name. If object 2 is enabled, print its name. If none of the previous conditions were true, print error message."
我在网上找到的所有使用多个 <c:when>
的例子都提供了相互排斥的条件,例如 n < 100
和 n > 1000
,或者在条件地点,例如<c:when test="condition 1 here">
和 <c:when test="condition 2 here">
。所以从我发现的例子中不清楚它是 if/else-if/else 还是 if/if/if-none-of-the-above.
来自 <c:choose>
tag documentation(强调我的):
Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when>
and <otherwise>
使用 jstl 核心标签库,我有多个条件要使用 <c:choose>
.
<c:choose>
<c:when test="${obj1.enabled}">
${obj1.name}
</c:when>
<c:when test="${obj2.enabled}">
${obj2.name}
</c:when>
<c:otherwise>
<spring:message code="error.NoRecordFound"/>
</c:otherwise>
<c:choose>
我读为 "if object 1 is enabled, print its name, else if object 2 is enabled, print its name, else print error message"。
但是 obj1
和 obj2
都可以启用,我担心它实际上会表现得像 "If object 1 is enabled, print its name. If object 2 is enabled, print its name. If none of the previous conditions were true, print error message."
我在网上找到的所有使用多个 <c:when>
的例子都提供了相互排斥的条件,例如 n < 100
和 n > 1000
,或者在条件地点,例如<c:when test="condition 1 here">
和 <c:when test="condition 2 here">
。所以从我发现的例子中不清楚它是 if/else-if/else 还是 if/if/if-none-of-the-above.
来自 <c:choose>
tag documentation(强调我的):
Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by
<when>
and<otherwise>