How to testing for enum equality in JSF?
Asked Answered
I

3

54

Is it possible to test for enum equality in JSF?

E.g. where stuff is an enum Stuff:

<h:outputText value="text" rendered="#{mrBean.stuff == mrsBean.stuff}"/>
Isodynamic answered 26/3, 2010 at 15:29 Comment(0)
S
103

This is actually more EL related than Faces related. The construct as you posted is valid, but you should keep in mind that enum values are in EL 2.1 or older actually evaluated as String values. If String.valueOf(mrBean.getStuff()) equals String.valueOf(mrsBean.getStuff()), then your code example will render. In EL 2.2 or newer the same construct will work, but they are evaluated as true enums.

Note that it indeed requires a getter method to return the enum value. Given the fact that enums are treated as String, you can in essence also just do:

<h:outputText value="text" rendered="#{mrBean.stuff == 'FOO'}" />

In EL, you cannot access enum values directly like this:

<h:outputText value="text" rendered="#{mrBean.stuff == Stuff.FOO}" />

This is only possible when you use Faces 2.3-introduced <f:importConstants> tag:

<f:metadata>
    <f:importConstants type="com.example.Stuff" />
</f:metadata>
...
<h:outputText value="text" rendered="#{mrBean.stuff == Stuff.FOO}" />

Or when you're not on Faces 2.3 yet or when you don't want to use <f:metadata>, use the OmniFaces predecesor <o:importConstants>:

<o:importConstants type="com.example.Stuff" />
...
<h:outputText value="text" rendered="#{mrBean.stuff == Stuff.FOO}" />
San answered 26/3, 2010 at 16:28 Comment(4)
I have a similar problem like what you mention: In managed bean I have Stuff stuff, and in my JSF I try #{stuff.FOO}, and it does not work. When u said a getter method the return enum values, can u be a bit more specific with the FOO, BAR example you have. I have made a separate question in case you want to have a better look at my structure. #3917371Morganatic
What way is preferable for this? Is importing OmniFaces only for this reason overhead?Scald
@Alex: the way which fits your application the best. As to OmniFaces, it offers way more than only the <o:importConstants>.San
@San Thank you. I choosed OmniFaces. Glad it worked so perfectly and setting up this is more than easy.Scald
E
38

If you have the enum

public enum Status {
    YES, NO
}

you can reference the enums in your jsf pages like so:

<h:outputText value="text" rendered="#{myBean.status == 'YES'}"/>

I'm not so sure about the String evaluation, due to something I stumbled upon while refactoring some code to use enums: if you have a typo in your status String, ie:

<h:outputText value="text" rendered="#{myBean.status == 'YESSIR'}"/>

you will actually get a runtime error when you hit the page because the EL parser will try to coerce 'YESSIR' into a Status enum and fail.

Edmonds answered 26/3, 2010 at 21:0 Comment(0)
C
2

You could define testing methods on the enum, see the following source.

Enum definition:

public enum MyEnum {
    FOO;
    public boolean isFoo(){
        return FOO.equals(this);
    }
}

JSF code:

<h:outputText value="text" rendered="#{mrBean.stuff ne null and mrBean.stuff.foo}"/>
Climb answered 9/6, 2015 at 15:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.