列表<Foo>作为使用 Spring 3 MVC 的表单支持对象,语法正确吗? [英] List<Foo> as form backing object using Spring 3 MVC, correct syntax?
本文介绍了列表<Foo>作为使用 Spring 3 MVC 的表单支持对象,语法正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想做这样的事情,其中 Foo
是一个具有一个 String 字段名称和 getter/setter 的类:
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"/>
然后提交带有更新名称的 Foos 的完整列表?
And then submit the complete list of Foos with updated names?
我的控制器如下所示:
@RequestMapping(value = "/FOO", method = RequestMethod.POST)
public String getSendEmail(List<Foo> foos, Model model) {
// ...
}
推荐答案
也许这可以回答你的问题:
Maybe this answersyour question:
控制器:
@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 列表包装器:
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 类:
public class Foo {
private String name;
public Foo() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
JSP 视图(名称 = 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>
这篇关于列表<Foo>作为使用 Spring 3 MVC 的表单支持对象,语法正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文