This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes <s:select               how to fill with key and value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "<s:select               how to fill with key and value " Watch "<s:select               how to fill with key and value " New topic
Author

<s:select how to fill with key and value

biny panackal
Ranch Hand

Joined: Apr 12, 2007
Posts: 72
Dear all,
First i am thanking you all who helped me to solve the problme.

Now i need to fill a select combo <s:select with key and value attribute. I am able to fill it with only values but no key. How can i set key value for each option content in ><s:select>



This is my select combo and i am listing the method that i used to fill the combo

private List day;
List qstCategory;
List qstCategoryKey;
String usrQst;






Waiting for a replay .


Regards,
Biny Balan
binypanackal@gmail.com


Biny Balan
binypanackal@gmail.com
biny panackal
Ranch Hand

Joined: Apr 12, 2007
Posts: 72
expecting replay
can i fill a <s:select using hashtable>
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Well if you use a java.util.List in the list attribute of the select tag, then the listKey and listValue both point to the values in the list. If the elements in the list are complex objects like for example a User class object with properties name and id, then you can set the listKey and listValue attributes to point to then like for example - listKey="id" listValue="name".

If you don't want that, then you must use a java.util.Map so that the listKey points to the keys in the map and listValue points to the values in the map...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
biny panackal
Ranch Hand

Joined: Apr 12, 2007
Posts: 72
So if i created a class

Sucategory{
String id
String value
}

and i add object of the same to the list

and if i give like="list" listKey="id listValie="value" wether it will work

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

biny panackal wrote:So if i created a class

Sucategory{
String id
String value
}

and i add object of the same to the list

and if i give like="list" listKey="id" listValie="value" whether it will work



I think it will work if you write listValue instead of listValie and the the list contains a list whose elements are of type Sucategory...
biny panackal
Ranch Hand

Joined: Apr 12, 2007
Posts: 72
I am using a class now with two field key and value





i filled to to list

List qstCategory


qstCategory = new ArrayList();
qstCategory.add(new AnsCategory("A","AAA"));
qstCategory.add(new AnsCategory("B","BBB"));
qstCategory.add(new AnsCategory("C","CCC"));

getter and setter method



public List getQstCategory(){
return qstCategory;
}



and for <s:select




I am not getting the proer value filled in the drop down
public void setQstCategory(List qstCategory){
this.qstCategory=qstCategory;
}




>
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Try adding javabean methods into AnsCategory class



Also why are you using this syntax

listKey="#qstCategory['key']"

This means that you are trying to access an attribute from the ActionContext. The listKey and listValue are string properties. Use this

biny panackal
Ranch Hand

Joined: Apr 12, 2007
Posts: 72
Thaks a lot . That worked fine.

But i still have some doubt in that concept. When we create a object. The values are assigned to the appropriate variables. when i am creating other with other the list hold object of diffrent values in each position. So why we need to use the bean property getter and settter since it will also do the same one.
 
 
subject: <s:select how to fill with key and value
 
Similar Threads
populate items using struts2 <s:select>
Dropdown creation dynamically from DB lookup
Something wrong with my <s:select>
DropDown Menu error in Struts 2
Help me out to list a table of conent in jsp using struts2