Showing posts from December, 2011

Automatic to-Object conversion in JSF selectOneMenu & Co.

When creating a web UI, there is often the need to let a user make a selection from e.g. a drop-down. The items in such a drop-down are not rarely backed by domain objects. Since these items in the HTML rendering are just simple character identifiers, we need some way to encode the object version of an item to this simple character identifier (its string representation) and back again. A well established pattern in JSF is the following: <selectOneMenu value="#{bean.selectedUser}" converter="#{userConverter}"> <selectItems value="#{bean.selectableUsers}" var="user" itemValue="#{user}" itemLabel="#{}" /> </h:selectOneMenu> With userConverter defined as something like: @Named public class UserConverter implements Converter { @Inject private UserDAO userDAO; @Override public Object getAsObject(FacesContext context, UIComponent component, String value) { return use