This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Struts and the fly likes JSF: HtmlSelectOneMenu and getting the values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "JSF: HtmlSelectOneMenu and getting the values" Watch "JSF: HtmlSelectOneMenu and getting the values" New topic
Author

JSF: HtmlSelectOneMenu and getting the values

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I have an HtmlSelectOneMenu on a form and have a binding to that object in the backing bean. What I need to do is get the value of the label for the SelectItem that is selected. There is a getValue() method but that only gets the value. So for example if I had

<option value="0">Dot Net Sucks</option>

getValue() returns "0". I need to get Dot Net Sucks. Anyone know how to go about doing this?


GenRocket - Experts at Building Test Data
Varun Khanna
Ranch Hand

Joined: May 30, 2002
Posts: 1400
The "UISelectItems" type of object can hold a map type of value, where in:

The keys of the map object (once converted to Strings) are assumed to be labels, and the values of this object (once converted to Strings) are assumed to be values, of SelectItem instances that will be constructed dynamically and added to the set of available options for the parent component, in the order provided by an iterator over the keys.

Source


Now your class can have a map object for generating the dynamic drop-down and the SelectOneMenu object.
The SelectOneMenu will return you the selected value, which you can use in the map object for finding out the label.

I havent used this so not sure if this will work actually.


- Varun
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I'm sure that will work. The problem I have then is I guess with the JSF implementation as it doesn't support a getLabel() method on the SelectOneMenu component. It doesn't make sense to have left that out. ASP.NET allows you to do this.

I even tried to use getChildren on the SelectOneMenu which gives me the UISelectItems but when I try and get UISelectItems children, which should objects of type SelectItem I get a 0 count on the number of children. But even if that worked, it was still way too much code.
Varun Khanna
Ranch Hand

Joined: May 30, 2002
Posts: 1400
Just curious, why do you require a label ? (unless your value-label combination forms a composite primary key in database, which seems highly unlikely since labels, I believe, are just more user friendly version of values)
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by K Varun:
Just curious, why do you require a label ? (unless your value-label combination forms a composite primary key in database, which seems highly unlikely since labels, I believe, are just more user friendly version of values)


I am providing a confirmation page for chosen form items. So when the user clicks Save they are presented the same form except the selectOneMenu's are replaced with outputText items and I need the Label to display to the user. The values are all numbers. And yes, I am using a value-label combo in my tables where the value is an int and the label is a string. The int's are stored as a foreign key in another table so that if I ever needed to change the label of an item I won't have to go and update thousands of entries in all different tables.
Varun Khanna
Ranch Hand

Joined: May 30, 2002
Posts: 1400
Gotcha !
 
GeeCON Prague 2014
 
subject: JSF: HtmlSelectOneMenu and getting the values