Easy Select Items for JSF
July 20, 2006 7 Comments
In order to use selection components like combos or listboxes, SelectItem objects are needed in JSF. There are two tags for the job in the spec. f:selectItem and f:selectItems. The singular one has itemLabel and itemValue attributes which also accepts value bindings other than static texts. The main problem is with the f:selectItems because it only gets a value attribute and does not have the itemLabel-itemValue attributes. This causes two problems.
* To display a collection in a page, you have to create a SelectItem collection from your data. Suppose you have a collection of customer objects and want to display the name-surname attributes as key-value in a combobox. The bad thing is that you need to iterate over the collection and create a new collection of SelectItem objects and return it to the component.
* By doing so, the domain model is coupled with the faces api.
Because of these two disadvantages, me and my buddy Mert created an extended version: s:selectItems. It has three new attributes; var, itemLabel and itemValue. By using this, the component automatically creates the Select Items collection from the given data. I dont think many people care about ibm’s jsf but it has a custom resolver to create the selectitems. Anyway I’ve deployed the example webapplication to my homepage at;
The component is also committed to Apache MyFaces Sandbox project by Martin and I believe it’s going to be promoted to tomahawk soon. Also an interesting thing occurred the day component joined sandbox, a user in myfaces list send a message saying the selectitems stuff is problematic in JSF, creating them takes time&effort and asked if an easier way exists in myfaces. Of course he is pointed to our new component in sandbox afterwards, seems the component joined at the right time. If you are not a myfaces user and want to use it anyway, check out the release at jsf-comp. I’ve also written a documentation there.