【问题标题】:How to use if, else condition in jsf to display image如何在jsf中使用if,else条件来显示图像
【发布时间】:2013-07-09 08:47:38
【问题描述】:

我有一个条件,我有一个注册表单,如果用户 ID 为 0,它应该显示虚拟图像,当我从任何更新编辑用户时,我检查用户 ID 是否不等于 0,然后显示相应的图像到用户 ID。

我在 jsf 页面中使用了 JSTL。但它总是试图去 else 循环来显示图像。该功能运行良好。唯一的问题是我第一次访问页面时无法显示虚拟图像。这是我的代码。:

<c:if test="${'#{user.userId}' == '0'}">
  <a href="Images/thumb_02.jpg" target="_blank" ></a>
  <img src="Images/thumb_02.jpg" />
</c:if>
<c:otherwise>
  <a href="/DisplayBlobExample?userId=#{user.userId}" target="_blank"</a>
  <img src="/DisplayBlobExample?userId=#{user.userId}" />
</c:otherwise>

我可以使用这个 JSTL 标签还是使用 jsf 来做?

【问题讨论】:

    标签: jsf-2 jstl


    【解决方案1】:

    对于像我这样刚刚通过 skuntsel 跟踪代码并收到神秘堆栈跟踪的人,请允许我为您节省一些时间。

    似乎c:if 本身不能跟c:otherwise

    正确的解决方法如下:

    <c:choose>
        <c:when test="#{some.test}">
            <p>some.test is true</p>
        </c:when>
        <c:otherwise>
            <p>some.test is not true</p>
        </c:otherwise>
    </c:choose>
    

    您可以根据需要添加额外的c:when 测试。

    【讨论】:

    • 很高兴你写了更新!刚刚更正了我的答案,以便它也可以编译。重读您的答案有时会有所帮助。
    • 另外值得注意的是 some.test 在应用请求值阶段不需要评估为 false。更多信息请参见此处(第 5 点):stackoverflow.com/questions/2118656/…
    【解决方案2】:

    嵌套 EL 表达式是非法的:你应该内联它们。在您的情况下使用 JSTL 是完全有效的。纠正错误,您将使代码正常工作:

    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:c="http://java.sun.com/jstl/core">
        <c:if test="#{not empty user or user.userId eq 0}">
            <a href="Images/thumb_02.jpg" target="_blank" ></a>
            <img src="Images/thumb_02.jpg" />
        </c:if>
        <c:if test="#{empty user or user.userId eq 0}">
            <a href="/DisplayBlobExample?userId=#{user.userId}" target="_blank"></a>
            <img src="/DisplayBlobExample?userId=#{user.userId}" />
        </c:if>
    </html>
    

    另一种解决方案是在一个元素的 EL 中指定您想要的所有条件。虽然它可能更重且可读性较差,但它是:

    <a href="#{not empty user or user.userId eq 0 ? '/Images/thumb_02.jpg' : '/DisplayBlobExample?userId='}#{not empty user or user.userId eq 0 ? '' : user.userId}" target="_blank"></a>
    <img src="#{not empty user or user.userId eq 0 ? '/Images/thumb_02.jpg' : '/DisplayBlobExample?userId='}#{not empty user or user.userId eq 0 ? '' : user.userId}" target="_blank"></img>
    

    【讨论】:

    • 这不仅会导致(几乎)重复代码,还会使评估时间加倍。请考虑使用c:choose + c:when + c:otherwise
    【解决方案3】:

    除了使用“c”标签,您还可以执行以下操作:

    <h:outputLink value="Images/thumb_02.jpg" target="_blank" rendered="#{not empty user or user.userId eq 0}" />
    <h:graphicImage value="Images/thumb_02.jpg" rendered="#{not empty user or user.userId eq 0}" />
    
    <h:outputLink value="/DisplayBlobExample?userId=#{user.userId}" target="_blank" rendered="#{not empty user and user.userId neq 0}" />
    <h:graphicImage value="/DisplayBlobExample?userId=#{user.userId}" rendered="#{not empty user and user.userId neq 0}"/>
    

    我认为这比 skuntsel 的替代答案更具可读性,并且使用 JSF 渲染属性而不是嵌套三元运算符。 答案是,您是否可能打算将您的图片放在锚标签之间,以便图片可点击?

    【讨论】:

      猜你喜欢
      • 2022-01-18
      • 1970-01-01
      • 2019-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-01
      • 2019-12-28
      • 1970-01-01
      相关资源
      最近更新 更多