tags:

views:

713

answers:

2

Is it possible to test for enum equality in JSF?

i.e.

<h:outputText value="text" rendered="#{myBean.enum==BeanEnum.enum1}"/>
+4  A: 

This is actually more EL related than JSF related. The construct as you posted is valid, but you should keep in mind that enum values are actually evaluated as String values.

If String.valueOf(myBean.getEnum()) equals String.valueOf(BeanEnum.getEnum1()), then your code example will render. Note that it indeed requires a getter method to return the enum value. You cannot access enum values directly in standard EL. So the following ain't going to work:

public enum Stuff { 
    FOO, BAR
}

with

#{stuff.FOO}

You can however workaround this by wrapping the enum in a bean or an EL function.

BalusC
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. http://stackoverflow.com/questions/3916871/passing-a-enum-value-as-a-parameter-from-jsf
Harry Pham
+3  A: 

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.

Naganalf