List<Foo> as form backing object using Spring 3 MVC, correct syntax?
36,829
Maybe this answersyour question:
CONTROLLER :
@Controller("/")
public class FooController{
//returns the ModelAttribute fooListWrapper with the 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 as many FOO you 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>
Author by
NimChimpsky
side hustle : metriculous.network Spring is too bloated, I created my own web app framework Infrequent tweets What if programming languages were methods to eat an orange?
Updated on July 09, 2022Comments
-
NimChimpsky almost 2 years
I want to do something like this, where
Foo
is a class with one String field name, and getter/setter:<form:form id="frmFoo" modelAttribute="foos"> <c:forEach items="${foos}" var="foo"> <form:input path="${foo.name}" type="text"/>
And then submit the complete list of Foos with updated names?
My controller looks like this:
@RequestMapping(value = "/FOO", method = RequestMethod.POST) public String getSendEmail(List<Foo> foos, Model model) { // ... }
-
littleK over 11 yearsI assume that this solution requires having a fixed amount of input fields, is that correct? What if you have a dynamic number of input fields? For example, I have a requirement to let my users dynamically generate new input fields. Do you know how to handle that situation?
-
w3bshark almost 11 yearsFaton, you're a life saver. Thanks a lot.
-
newbie over 10 yearsFaton, can you explain a bit why do you need to write a wrapper class?
-
newbie over 10 yearsI tried to follow this code but I'm getting
Neither BindingResult nor plain target object for bean name 'com' available as request attribute
error -
cmaduro over 9 yearsHow would you do this in Thymeleaf?
-
Clemzd almost 9 yearsIs there a new solution with Spring 4? Because in this example it is needed to write "listwrapper" which is quite boring ^^
-
Joel Peltonen about 7 yearsHey. What does
<form:input path="fooList[${i.index}].name" type="text"/>
look like rendered? I need to re-create it with javascript :) -
Joel Peltonen about 7 yearsOh, and by the way, see
public String getFooList() {
andpublic void setFooList(String fooList) {
datatypes, they look very wrong, are they? -
sbsatter about 7 years@Nenotlep, I don't understand what you meant with your first comment. Could you be more clear? And thank you for pointing that out, the answer has been edited. Btw. if you are asking what it looks like when viewed in a browser, it's simply the java way (correct me if I'm wrong) of the following: <input name="" type=""/>.
-
Joel Peltonen about 7 yearsYes, that is what I meant; what does it look like to the browser. So the browser sees
<input name="" type=""/>
? Are the name and type empty? -
sbsatter about 7 yearsSorry for such a late reply, no they are not empty. type is text but the name and id both is the same as the value stored in fooList[0].name...fooList[n].name.