Java Notes From My Desktop

Tags - Categories : All | Articles | Book Reviews | CSS | Java | Javaranch | Javascript | News | Opengl | Opinions | Personal | php

I like JSF. I wondered what took Sun so long to get it done. But as I have been using it on a current project I have begun to see some ugliness that is JSF. Over the next several days and/or weeks I will be posting issues I am having with JSF and hopefully how I have decided to overcome them so that others can learn from my little bit of experience.

The first thing I'd like to talk about is the UISelect components (UISelectMany, UISelectOne, etc). I built a form with several HtmlSelectOneMenu objects. To fill these lists I pull information from a database. The tables in the database have an Id and a Label. After binding an HtmlSelectOneMenu object in my managed bean using the binding attribute in my JSP I needed to get the label of the item that was chosen in the list. Well, you can't. At least not easily.

Oddly enough the UISelect objects have a getValue() method that returns the value attribute of the html option tag but there is no getLabel() or getName() to get the label associated with that option. JSF is competing with ASP.NET isn't it? ASP.NET has the ability to get the label from the Select object. JSF doesn't.

The easiest approach to getting this label is to look it up in your model. For example, I have an ArrayList filled with SelectItem objects that hold the SelectItems for a list of hardware. So in my backing bean I have a getter and setter for this ArrayList. In whatever method I get they SelectItem from the ArrayList at the index of whatever value I get from the getValue() method of my HtmlSelectOneMenu object.

Of course now there is some nasty looking code because I have to cast to a SelectItem and then cast out to a Hardware object and then call a getHardware() method just to get my label. Ugh!

If anyone knows of a better way of doing this, please share it with me. For now I have a solution that works but it isn't pretty.