javascript - 你可以使用<c :otherwise> with <c:if> or only with <c:when>?

标签 javascript jsp jsf jstl jsp-tags

我在 StackOverflow 中搜索了 <c:if><c:otherwise>并找到了一些信息,但不完全是我正在寻找的信息。 <c:otherwise>看起来设计如下:

<c:choose>
    <c:when test="${somecondition}">
        <%-- do stuff --%>
    </c:when>
    <c:when test="${someothercondition}">
        <%-- do other stuff --%>
    </c:when>
    <c:otherwise>
        <%-- do other other stuff --%>
    </c:otherwise>
</c:choose>

(基本上就像一个 switch 语句)。

但是,我发现的其他问题都无法明确告诉我这是否是您可以使用 <c:otherwise> 的唯一方法。 (尽管他们似乎确实暗示了这一点)。可以吗

<c:if test="${somecondition}">
        <%-- do stuff --%>
</c:if>
<c:otherwise>
    <%-- do other stuff --%>
</c:otherwise>

(就像 if/else)或者这是错误的?

(请检查我的标签。我必须同时使用/学习 JSP、Javascript 和 JQuery,因此有时会混淆哪个新东西是哪个新语言。)

最佳答案

没有 <c:choose /> 就无法使用它。 JSTL 简单条件标记和条件 block 执行的不同之处:

The if tag allows the conditional execution of its body according to the value of the test attribute.

The choose tag performs conditional block execution by the embedded when subtags. It renders the body of the first when tag whose test condition evaluates to true. If none of the test conditions of nested when tags evaluates to true, then the body of an otherwise tag is evaluated, if present.

所以你肯定需要一个条件 block ( <c:choose /> )才能使用 <c:otherwise /> .

另请参阅:

关于javascript - 你可以使用<c :otherwise> with <c:if> or only with <c:when>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21460026/

相关文章:

javascript - 谁能解释为什么这个 IIFE 传入的参数会改变作用域内的变量,使其变为未定义的?

javascript - 如何将条件包含到组合的 JSP 和 JavaScript 函数中?

java - 托管 Bean - 仅在页面加载时执行代码

javascript - 输入表单字段中的背景图像以清除焦点

javascript - 循环更改背景图像

javascript - 拖放 HTML 有限制

html - 通过 JSP 页面中的超链接传递值

javascript - 如何让下面的jquery代码更加高效

java - 如何要求我的 JSF 实现通过 Flash 重定向到新 View ?

jsf - 用户界面 :include can not include pages with different server context