Is it possible to test for enum equality in JSF?
i.e.
<h:outputText value="text" rendered="#{myBean.enum==BeanEnum.enum1}"/>
Is it possible to test for enum equality in JSF?
i.e.
<h:outputText value="text" rendered="#{myBean.enum==BeanEnum.enum1}"/>
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.
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.