views:

2648

answers:

3

I have an bean placed on my action Event.java, the action is Called ManageEvents. I would like the user to be able to add to a struts2 multiple select form field and create a list or map of items (in this case Map where the data would be .

<struts2:select name="event.dj_map" label="Add DJs To Your Event" list="event.dj_map" listsize="5" multiple="true" headerKey="headerKey" required="true" headerValue="--- Please Select ---">

So ultimately I would like to know how to pass a mult. select field (e.g. Events.dj_map) as a map of name,value pairs to an object (e.g. Event.java) set on the action (e.g. ManageEvents.java).

A: 

I've never done it as Map, and I'm not sure you can, but if you have this code in your Events class, it will be retreived on page load and populated with a List on submit:

public class Events {

   private List dj_map = new ArrayList();  

   public List getDj_map(){
      return dj_map;
   }

   public void setDj_map(List dj_map){
      this.dj_map = dj_map;
   }
}

Once you get the above working, you could always try changing the type of dj_map to Map to see if Struts populates it properly.

And as a style note, the name of the variable should really be djMap, but hey, the code will still work just fine with the underscore.

Pat
A: 

The http request which is generated from a select with multiple=true will look like: name=option1&name=option2&name=option3 etc.

The Struts2 ParametersInterceptor will then pick this up an try to set your property either with String[] or any kind Collection.

In order to submit it onto a Map, it would have to know what to use for keys/values in that map (which isn't really possible) given the above request.

So I don't think you can directly have Struts2 submit your multiple options onto a Map.

Horia Chiorean
A: 

1) Define a Map in your action, e.g.:

public class Events {

   private Map<Integer,Integer> dj_map = new HashMap<Integer,Integer>();  

   public Map<Integer,Integer> getDj_map(){
      return dj_map;
   }

   public void setDj_map(Map<Integer,Integer> dj_map){
      this.dj_map = dj_map;
   }
}

2) Pass parameters using map notations.

<input type="text" name="dj_map[#anyIntegerValue]" />
<input type="text" name="dj_map[#anyIntegerValue]" />

Here #anyIntegerValue can be any value that you want to use as key.

mahesh