java - Populate List<String> in struts2 from form data -


i feel should exceedingly obvious, far i've failed find answer.

i want have list of strings (or array of strings, don't care) populated form data in struts2.

i've seen several examples of how indexed properties beans, wrapping single string inside object seems silly.

so have like

    public class controller extends actionsupport {          private list<string> strings = new arraylist<string>();         public controller() {             strings.add("1");             strings.add("2");             strings.add("3");             strings.add("4");             strings.add("5");         }         public string execute() throws exception {             return actionsupport.success;         }         public list<string> getstrings() {             return strings;         }          public void setstrings(list<string> s) {             strings = s;         }     }     

...

<s:iterator value="strings" status="stringstatus">    <s:textfield name="strings[%{#stringstatus.index}]" style="width: 5em" /> </s:iterator> 

the form fields populated initial values (e.g. 1, 2, etc), results not posted back. setstrings never called, values set empty strings.

anybody have idea what's going on? in advance!

i believe have it, jsp code render like:

<input type="text" name="strings[0]" style="width: 5em" value="1"/> <input type="text" name="strings[1]" style="width: 5em" value="2"/> <input type="text" name="strings[2]" style="width: 5em" value="3"/> ... 

notice name of field references "strings[x]" need name "strings". suggest like:

<s:iterator value="strings" status="stringstatus">    <s:textfield name="strings" value="%{[0].tostring()}" style="width: 5em" /> </s:iterator> 

not sure if value attribute above may correct, think desired result.


Comments