如何使用If ..... Else在JSP页面中使用JSTl标记

时间:2013-08-21 10:17:40

标签: jsp jstl

我从动作类中获取状态消息。基于状态消息iam显示两个不同的图像。

如果Status消息为“Acces denied”,则它会在JSP页面中显示一个图像。 否则,我们想要展示不同的图像。

3 个答案:

答案 0 :(得分:6)

令人惊讶的是,JSTL is documented,并且official tutorial。谷歌是你最好的朋友。

你正在寻找c:选择,c:何时和c:否则:

<c:choose>
    <c:when test="${status.message == 'Access Denied'}">
        ...
    </c:when>
    <c:otherwise>
        ...
    </c:otherwise>
</c:choose>

答案 1 :(得分:5)

没有像jstl中的if else那样的条件而不是jstl提供以下

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

有关详细信息,请参阅此link

答案 2 :(得分:0)

JSTL附带<c:choose><c:when><c:otherwise>标记,以在JSP中实现if-then-else类型逻辑。查看JSTL Core Choose Tag了解基本示例。

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