Getting Description from HtmlSelectOneRadio

Hi, I am reading values from UI Components, for example radio button,

I tied the values to SelectedItems and added value,descritpoion,

I am getting value by using

public HtmlSelectOneRadio getSelectOneRadio() {

if(selectOneRadio == null){

selectOneRadio = (HtmlSelectOneRadio)findComponentInRoot("selectOneRadio");

}

return selectOneRadio;

}

but any idea how to get Description ?

Thanks

Sriaknth

[468 byte] By [srikanthga] at [2007-11-27 4:07:08]
# 1

I don't know if I understand your question... But this is what I think you are asking for.

HtmlSelectOneRadio selectRadio = getSelectOneRadio();

SelectItem selectItemValue = (SelectItem)selectRadio.getValue();

String description = selectItemValue.getDescription();

Are you sure it's the description you want though? Usually people only want the label or value. It's rare to see the description used. Anyways, once you've got the SelectItem out of the component, it's pretty easy to get the label, value or description.

CowKing

IamCowKinga at 2007-7-12 9:12:20 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

but when I am reading , the value contains the String, like example

SelectItem selectItem = new SelectItem("test","case");

so when I displayed on page it showing case, when I retrive the value it contains the test,

so I need both values to return.

if i do, getSelectOneRadio().getValue() -than it returning the test, and I want to retrive case also.

as u provided code its giving ClassCastExcep., as the value is not SelectItem .

Thanks

Srikanth

srikanthga at 2007-7-12 9:12:20 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

You cannot retrieve it by the selected value -which is just an ordinary String.

A better practice is to maintain a backing map with the key-value pairs so that you can use this map to re-retrieve the labels in the action method. If you're using a collection of DTO's, then you can also just put them in the SelectItem value. If those DTO's have a readable toString() which can be used as label, then you can leave the SelectItem label away.

By the way, I also guess that you have to read something more about the "correct use" of the 'value' and 'binding' attributes of the components. The way you're trying to get the values is just kind of an roundabout way.

BalusCa at 2007-7-12 9:12:20 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Whops! Yes, of course it only returns the String value. My mistake, I had a brain lapse there. =)Balus is correct, it's easiest and best to use a map (such as hashmap) to maintain a list of key/values (or in SelectItem terms, value/label) in your SelectItems.CowKing
IamCowKinga at 2007-7-12 9:12:20 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...