JSF 2: Using enums in the rendered attribute

Theo picture Theo · Jan 16, 2011 · Viewed 22.2k times · Source

Is there any way to check declaratively whether an enum has a specified value. For example:

<h:graphicImage name="error.png" library="images" 
  rendered="#{viewController.current.status == Status.ERROR}" />

It's a little bit tedious to define a method in the managed beand that checks this for every enum value, e.g.

public boolean isStateIsError() {
  return current.getStatus() == Status.ERROR;
}

Is there a shorter/better way of doing this?

Answer

BalusC picture BalusC · Jan 16, 2011

Until EL 3.0 it's not possible to import enums in EL scope. You can however just treat and compare them like strings, i.e. the enum constant value must be quoted like below.

<h:graphicImage name="error.png" library="images" 
  rendered="#{viewController.current.status eq 'ERROR'}" />