jsp - List<Foo> as form backing object using spring 3 mvc, correct syntax? -
i want this, foo class 1 string field name, , getter/setter :
<form:form id="frmfoo" modelattribute="foos"> <c:foreach items="${foos}" var="foo"> <form:input path="${foo.name}" type="text"/>
and submit complete list of foos updated names ? controller sig looks :
@requestmapping(value = "/foo", method = requestmethod.post) public string getsendemail(list<foo> foos, model model) {}
maybe answersyour question:
controller :
@controller("/") public class foocontroller{ //returns modelattribute foolistwrapper view fooform @requestmapping(value = "/foo", method = requestmethod.get) public string getfooform(model model) { foolistwrapper foolistwrapper = new foolistwrapper(); foolistwrapper.add(new foo()); foolistwrapper.add(new foo()); //add many foo need model.addattribute("foolistwrapper", foolistwrapper); return "fooform"; } @requestmapping(value = "/foo", method = requestmethod.post) public string postfoolist(@modelattribute("foolistwrapper")foolistwrapper foolistwrapper, model model) { //........... } }
foo list wrapper :
public class foolistwrapper { private list<foo> foolist; public foolistwrapper() { this.foolist = new arraylist<foo>(); } public list<foo> getfoolist() { return foolist; } public void setfoolist(list<foo> foolist) { this.foolist = foolist; } public void add(foo foo) { this.foolist.add(foo); } }
foo class :
public class foo { private string name; public foo() { } public string getname() { return name; } public void setname(string name) { this.name = name; } }
jsp view (name = fooform):
<c:url var="foourl" value="/foo"/> <form:form id="frmfoo" action="${foourl}" method="post" modelattribute="foolistwrapper"> <c:foreach items="${foolistwrapper.foolist}" varstatus="i"> <form:input path="foolist[${i.index}].name" type="text"/> </c:foreach> <button>submit</button> </form:form>
Comments
Post a Comment