使用< c:当>与枚举 [英] Using <c:when> with an enumeration

查看:107
本文介绍了使用< c:当>与枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSP portlet需要根据具有枚举类型

I have a JSP portlet that needs to display different markup according to the value of a bean's property which is of an enumeration type

public enum State {
    CANCELED, COMPLETED
}

以下代码进行切换

<c:choose>
    <c:when test="#{item.state == 'COMPLETED'}">
        <img src="ok.gif" />
    </c:when>
    <c:when test="#{item.state == 'CANCELED'}">
        <img src="ko.gif" />
    </c:when>
</c:choose>

但它不起作用。有趣的是,在这两种情况下都返回false。项目对象(ICEFaces数据表内)是具有状态 getter + setter属性的支持bean。
我被告知要将枚举与字符串进行比较,并使用 == 运算符,但也许不是这样。

but it doesn't work. Interestingly enough, it returns false in both cases. The item object (inside an ICEFaces data table) is a backing bean with a State getter+setter property. I have been told to compare the enumeration to a string and use the == operator, but maybe that's not the way.

所以,我的问题是:如何使用& lt; c;当& gt; 标签将属性与枚举值进行比较?

So, my question is: how do I use the &lt;c:when&gt; tag to compare a property to an enumeration value?

推荐答案


... 项目对象(ICULaces数据中的 ...

... The item object (inside an ICEFaces data table) ...

然后JSTL确实不起作用。它在视图构建时运行,而不是在视图呈现时间。基本上,您可以将其可视化如下:JSTL首先从上到下运行,然后将包含JSF标签的生成结果交给JSF,JSF再次从上到下运行。现在,JSTL遇到迭代的JSF datatable #{item} ,它是 null ,因此它总是会避免 false ,JSF将不会从JSTL中检索出这些图像。

Then JSTL indeed doesn't work. It runs during view build time, not during view render time. Basically you can visualize it as follows: JSTL runs from top to bottom first and then hands over the generated result containing JSF tags only to JSF which in turn runs from top to bottom again. At the moment JSTL encounters the iterated JSF datatable #{item}, it is null and thus it will always evaulate false and JSF will retrieve neither of those images from JSTL.

您要使用JSF代码。我建议< h:graphicImage> 结合呈现属性。

You want to use a JSF tag instead. I'd suggest <h:graphicImage> in combination with the rendered attribute.

<h:graphicImage value="ok.gif" rendered="#{item.state == 'COMPLETED'}" />
<h:graphicImage value="ko.gif" rendered="#{item.state == 'CANCELED'}" />

这篇关于使用&lt; c:当&gt;与枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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