Monday, September 17, 2007

Seam, Enums and Select Boxes

While building a form I wanted the option elements inside a select to reflect the values of an Enum that I had.

I tried giving an @Name annotation to the Enum, which didn't work.  This post on the JBoss forums explained that Enums can't be instantiated and therefore can't be used as a Seam component.  I felt sort of dumb for trying that and decided to implement the suggested solution.

I built the piece I was working on from the "stateful" example from Michael Yuan's book, Jboss Seam : Power and Simplicity Beyond Java EE (Amazon link). My form was on "main.xhtml" and was submitting to "ManagerAction" which implemented "Manager" interface.  The Enum I wanted for the select was "ServiceTicketStatus." 

I added the following to my ManagerAction class,

   public ServiceTicketStatus[] getServiceTicketStatuses()
      return ServiceTicketStatus.values();

and this to the Manager interface,

public ServiceTicketStatus[] getServiceTicketStatuses();

and this to the form in main.xhtml,

<h:selectOneMenu id="searchStatus"  value="#{manager.statusSearch}" >
    <f:selectItem itemValue="" itemLabel="Please Select" />
    <s:selectItems value="#{serviceTicketStatuses}" var="statuses" />

It worked as advertised.

Blogged with Flock