• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSF: HtmlSelectOneMenu and getting the values

 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gotcha !
 
Are you okay? You look a little big. Maybe this tiny ad will help:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic