如何使用if,else条件在jsf中显示图像 [英] How to use if, else condition in jsf to display image

查看:66
本文介绍了如何使用if,else条件在jsf中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个条件,即我有一个登记表,其中如果userid为0,则应显示虚拟图像;当我从任何更新中编辑用户时,我检查userid是否等于0,然后显示对应的图像到用户ID.

I have a condition where I have an enrollment form in which if userid is 0 it should show the dummy image and when I edit user from any update, I check for if userid which is not equal to 0 then display the image corresponding to userid.

我在jsf页面中使用了JSTL.但始终会尝试转到else循环以显示图像.该功能运行正常.唯一的问题是,当我第一次访问该页面时,我无法显示虚拟图像.这是我的代码.

I used JSTL inside jsf page. But always it tries to go to else loop for showing image. The functionality is working fine. The only thing is I can't display the dummy image when I visit the page first. Here s my code.:

<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进行标记?

Can I use this JSTL tag or can I do it using jsf?

推荐答案

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

It is illegal to nest EL expressions: you should inline them. Using JSTL is perfectly valid in your situation. Correcting the mistake, you'll make the code working:

<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内指定所需的所有条件.尽管它可能较重且可读性较差,但它是:

Another solution is to specify all the conditions you want inside an EL of one element. Though it could be heavier and less readable, here it is:

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

这篇关于如何使用if,else条件在jsf中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆