Archive for March, 2007

JSF/MyFaces f:selectItems and automatic converters

JSF converters provide a way for mapping complex Java types to the UI. A class converter is registered for a type and is then called every time an object of the given class is rendered to the UI (or posted from a form), unless another converter is specified. For example, registering the following converter in your faces-config.xml provides automatic conversion of Enum values (a similar converter is already included in JSF 1.2, but missing in 1.1):

<converter>
<converter-for-class>java.lang.Enum</converter-for-class>
<converter-class>com.dlicht.converter.EnumConverter</converter-class>
</converter>

With this, you can for example use Enums for selectItem values. What I did not fully realize and what cost me hours before I stepped through the MyFaces source is that the converter is determined through the input component’s value mapping type, not the value type of the selectItems themselves. Although this does make sense (because it’s the bean property that represents the the component value, and not the current selectItem, and the selectItem values of a select list don’t even have to be of the same type), it’s not entirely trivial. For example,

<h:selectOneListbox value=”#{myBean.enumProperty}”>
<f:selectItems value=”#{myBean.enumValues}”/>
</h:selectOneListbox>

works without an explicit converter, but

<h:selectOneListbox>
<f:selectItems value=”#{myBean.enumValues}”/>
</h:selectOneListbox>

does not.

Comments (1)

Festival summer…

…is getting better. The Beastie Boys are coming to the Nuke festival near Vienna in July – their first concert in Austria since almost 10 years. Although I haven’t listened to them regularly for several years, they’re still one of my favorite bands. As is Nine Inch Nails, who will play in Vienna on March 29.

Leave a Comment