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,

@Factory("serviceTicketStatuses")
   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" />
</h:selectOneMenu>

It worked as advertised.

Blogged with Flock

Wednesday, September 12, 2007

Another cool thing about JBoss Seam : The s:selectItems tag

I've been using JBoss Seam and reading JBoss Seam : Power and Simplicity Beyond Java EE (Amazon link). I've been working through the examples from Michael Yuan's site and tweaking the "Stateful" example into something resembling my current project.

I just came across the Seam JSF Controls.  The s:selectItems tag is really nice.  It converts a List of Objects, in my case a List<String> into a List<SelectItem>.

The more I use Seam the more impressed I am with the way it addresses the headaches of JSF and web development.

Blogged with Flock