We Solve Problems

Using Enums in EL

By , 21 January 2009

Using Enums in EL

Unfortunately EL has no special support for enums. That makes it especially hard when you need to input enum values (for example from a dropdown box). One simple solution is to add a getter and setter to your bean to marshall the String value to and from an enum:

enum Status { A, B, C, D}

/* EL convenience getters and setters */
public String getStatus_() {
    return (status == null) ? null : status.name();
public void setStatus_(String status) {
    this.status = Status.valueOf(status);

Now you can build your dropdowns easily like this:

<h:outputText value="Status:"/>
<h:selectOneMenu value="${bean.status_}">
  <f:selectItem itemValue="A" itemLabel="Asleep"/>
  <f:selectItem itemValue="B" itemLabel="Bored"/>
  <f:selectItem itemValue="C" itemLabel="Creative"/>
  <f:selectItem itemValue="D" itemLabel="Dead"/>

In this example I use the underscore on the method name to distinguish it from the regular getter and setter which are not shown.

Using Enums in EL

About Roger Keays

Using Enums in EL

Roger is an active member of the Java Community and a previous contributor on the JSF Expert Group. He has been writing software since the age of 8 and his other interests include languages, psychology, travel and running. If you're feeling game, you can also explore his creative side.

Add a comment

Please visit http://www.NinthAvenue.com.au/blog/using-enums-in-el to add your comments.

Join The Mailing List

Subscribe to our mailing list for the latest news and announcements.

Follow Ninth Avenue

Website Updates