9 Dec

JSF: Use Enum values for selectItems

When using elements like radio buttons or drop down menus in JSF you usually specify the items which can be selected. There are in general two possiblities:

  • Using the <f:selectItem> tag which represents one item
  • Using the <f:selectItems> tag which refers to a collection (list, map, array, …) in a backing bean

The latter is very useful when handling with dynamic content, i.e. the number of elements is not fix. Usually a select item has a value associated with a label. When an item is selected the value will be handed over to the bean. The label is the string representation shown to the user in the UI. In this post I will show a way to use enums as select items in a radio button menu. I will start right away with the JSF example followed by the definition of the enum and the right binding of the radio button to the backing bean’s corresponding field.

The radio button menu in JSF could look like the following:

<h:selectOneRadio value="#{MyController.selectedItem}" style="margin:auto;" >
     <f:selectItems value="#{MyController.items}" />
</h:selectOneRadio>

The enum used as selections for the radio button could be defined as follows:

public enum RadioButtonItem {
	FIRST_ITEM("First item"), SECOND_ITEM("Second item");
	
	private final String label;
	
	private SearchLevelItem(String label) {
		this.label = label;
	}
	
	public String getLabel() {
		return label;
	}
}

For each enum value we specify a corresponding label which will be used as string representation of the value in the UI.

In the bean MyController the list of possible items will be created and it holds the value holding the currently selected item:

@ManagedBean(name = MyController.beanname)
@SessionScoped
public class SearchController {
     public static final String beanname = "MyController";
     ...
     private SelectItem[] items;
     private RadioButtonItem selectedSearchLevel;

     public MyController() {
          ...
          items= new SelectItem[SearchLevelItem.values().length]; 
		for(int i = 0; i < RadioButtonItem.values().length; i++) {
			items[i] = new SelectItem(RadioButtonItem.values()[i], RadioButtonItem.values()[i].getLabel()); 
		}
		
		selectedItem = RadioButtonItem.FIRST_ITEM;
     } 
 
     ...
     public SelectItem[] getSearchLevelItems() {
	return searchLevelItems;		
     }

     public void setSelectedItem(String selectedItem ) {
	this.selectedItem = RadioButtonItem.valueOf(selectedItem);
     }
	
     public String getSelectedItem() {
	return selectedSearchLevel.toString();
     }

The controller holds an array of SelectItem objects which will be initialized in the constructor. As value we put in the enum value and as label the according label we specified in the RadioButtonItem enum. It further holds a field holding the selected Item. The setter's parameter will be a String object as HTML is text and therefore you will receive a string representation of the enum value. If you do not use a String object (but an RadioButtonItem value instead for example) you will probably get a property not writeable exception (something similar to class javax.faces.component.UpdateModelException /s/tabs/yourPage.xhtml @... value="...": Property '...' not writable on type java.lang.String). The getter method will return a String (which is the enum value as string). It is essential that anything you send to a web page is convertible to text format since HTML is text.

Ressources
http://stackoverflow.com/questions/2868778/jsf-2-0-use-enum-values-for-selectonemenu
http://www.coderanch.com/t/535974/JSF/java/UpdateModelException-Property-xx-writable-type

Leave a Reply

Your email address will not be published. Required fields are marked *


+ two = 6

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" extra="">