Creating SelectItem lists for dropdown and list controls is a pretty boring task. Everytime you do it, it’s like why isn’t this already part of JSF - it’s the same stupid task again and again.
So I was like - I should help myself and create something reusable, a component! I have been using JSF now for a while but only slowly I’m starting to write my own JSF components (creating a JSF component is a pretty heavy task, this should get easier in future releases of the JSF specification!).
This time it came to my mind that JSP allows to create EL functions that only require a class with a static method, a tag library descriptor and can be packaged easily into a JAR. So I’d wish a function that can be used in a JSF JSP to convert any collection like type such that JSF can display it through one of its selectOne, selectMany, etc. components.
So the selectItem function would just do it’s job and no longer my presentation logic would get polluted with all this collection iterations and SelectItem instantiations.
Sounds interesting, but what is required to implement this function? First we need a tag library descriptor that is dropped somewhere into the WEB-INF folder. And we need the Java code that is actually doing all the heavy job.
Well, while implementing this stuff I found several other solutions. JBoss Seam is providing some enhanced JSF controls like MyFaces. If you don’t want to pull in their libraries I guess my solution is pretty compact and easy to deploy as well!