Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Struts 2 »

[S2] How to use a Map backed form?

Eric Rank

2006-12-25


Hi all

Using Map backed forms in Struts 1 was really great. I'm wondering
how it works in Struts 2. Looking at the Type Conversion docs (http://
struts.apache.org/2.x/docs/type-conversion.html#TypeConversion-
CollectionandMapSupport) seem to get close to explaining it, but
jumps right into a complex indexed form field example.

Ultimately, I can't get the Map in my Action to populate from the
form correctly, and I'd like to know how to do it. I'm attempting to
generate a bunch of dynamically named textfields like this:

<s:iterator value="%{fields}" id="field">
 <s:textfield name="myField[%{field}]" />
</s:iterator>

This gives me fields that look like the following, which I expect:

<input type="text" name="dynaField[firstField]" ... />
<input type="text" name="dynaField[secondField]" ... />
<input type="text" name="dynaField[thirdField]" ... />
<input type="text" name="dynaField[etc]" ... />

The Action that the form submits to looks something like this;

class MyAction extends ActionSupport{

 private Map<String,String> dynaField = new HashMap<String,String>();

 public String execute()throws Exception{
   //when I get here, the dynaField property is a valid HashMap that
looks like
   //{null=the value of an unpredictable field}
   System.out.println(dynaField);
   return  SUCCESS;
 }

 //Getter and Setter here
 public void setDynaField(Map<String,String> map){ dynaField = map; }
 public Map<String,String> getDynaField(){ return dynaField; }
}



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@(protected)
For additional commands, e-mail: user-help@(protected)

©2008 junlu.com - Jax Systems, LLC, U.S.A.