tags:

views:

221

answers:

2

Hi all,

You can easily use a List in struts2 select tag, but is there a way to use Map in tag?? If it is possible please provide a sample code...

thanx !

+1  A: 

It depends on what are you trying to do. Lacking details, I can only point you to the docs : the list attribute of the select tag is an ...

Iterable source to populate from. If the list is a Map (key, value), the Map key will become the option 'value' parameter and the Map value will become the option body.

Below in the same doc there is an example with a (literal, inline) map (Months).

leonbloy
Leonboy's suggestion is optimal if you have literal values to add to a list. How are you generating the list? Do you have a map that you are trying to load? In that case you can pass the action variable referencing the map to the "list" attribute. Look at my answer for that example.
Kartik
A: 

In my action class

public class MyAction extends ActionSupport {
   private Map<String, String> map;

   public String execute() throws Exception {
       map = new HashMap<String, String>();
       map.put("abc", "abc");
       map.put("xyz", "xyz");
       return SUCCESS;
   }
}

For the jsp mapped to success, use some thing this

<s:select list = "map" name = "name" label = "Name" headerKey="" headerValue = "Enter Value"/>
Kartik