Enum backed h:selectOneRadio and h:selectOneMenu

Recently I needed to display a h:selectOneRadio and a h:selectOneMenu with values provided by a Java Enum. After some research on seamframework forum I saw one post that clarified a lot the way I should pursue, specially Pete’s comment. My greatest concern was the same of Pete: wiring up presentation and domain layer. But, after some analysis I found a simple solution that does not wire up domain and presentation layer and also respects the DRY principle. The solution involved crafting a class that would be instantiated as a Seam component on components.xml so it can be instantiated for each and every enum that we want to provide on view layer and also used EnumSet.allOf method so we could automagically iterate for each of the enum values.

Below, you can find the EnumList class:

EnumList Seam Component - Converts Java Enums to List.
Copyright (C) 2011 Rafael Ribeiro

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public class EnumList<T extends Enum<T>> {
private List<T> list;

public void setEnumClass(Class<T> c) {
list = new ArrayList<T>();

public List<T> getList() {
return list;

public String toString() {
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
.append("list", list).toString();


And the configure it on components.xml as follows:

<component name="myEnumComp" scope="application" auto-create="true">
<property name="enumClass">br.com.rafaelri.MyEnum</property>
<factory name="myEnum" value="#{myEnumComp.list}" scope="application" auto-create="true"/>

Finally you’ll refer to it on your xhtml as follows:

<h:selectOneMenu id="myEnumSelect" value="#{instanceHome.instance.myEnum}">
<s:selectItems var="enum" value="#{myEnum}" label="#{enum.description()}"/>
<s:convertEnum />

3 Responses to “Enum backed h:selectOneRadio and h:selectOneMenu”

  1. 1 Fhighlander
    May 21, 2011 at 6:50 pm

    Good post as always =)

    I would like to know more about those Java EE frameworks. Usually CDI and IoC are good news, but I really need to use those guys? When should I use or not?

    For example: in those Web projects that deal with basic CRUD and a couple of funcionalities, is it better to use Java in side server and another RAD Framework like Flex or Rails on client side?

    Sometimes I keep thinking myself but I don’t know how to answer this…

    • May 21, 2011 at 11:48 pm

      The answer is the regular one: DEPENDS!
      Be careful not to suffer from an illness that Maslow and Kaplan documented its symptom: “if all you have is a hammer, everything looks like a nail”.
      If you have plenty of components, their dependencies are expressed upon interfaces and you can have multiple implementations, etc… then you probably need DI to simplify component instantiation and composition. But if you have a rather simple scenary those frameworks may end up being more complex than doing it manually.


    • May 21, 2011 at 11:51 pm

      Forgot to reply one part:
      mixing frameworks or technologies is still not that simple. If you use some lightweight integration technique like JSON you may make this simpler. But remember that sometimes those technologies have some hidden tricks that may present hidden threats (eg.: GWT and Hibernate proxies).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


Blog Stats

  • 374,620 hits since aug'08

%d bloggers like this: