Nim*_*sky 20 spring jsp jstl spring-mvc
我想做这样的事情,其中foo是一个带有一个String字段名的类,以及getter/setter:
<form:form id="frmFoo" modelAttribute="foos">
<c:forEach items="${foos}" var="foo">
<form:input path="${foo.name}" type="text"/>
Run Code Online (Sandbox Code Playgroud)
然后提交更新名称的Foos完整列表?我的控制器sig看起来像这样:
@RequestMapping(value = "/FOO", method = RequestMethod.POST)
public String getSendEmail(List<Foo> foos, Model model) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
Fat*_*ton 33
也许这回答你的问题:
控制器:
@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) {
//...........
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
FOO CLASS:
public class Foo {
private String name;
public Foo() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
34234 次 |
| 最近记录: |