使用Spring 3 MVC将List 作为表单支持对象,正确的语法?

使用Spring 3 MVC将List 作为表单支持对象,正确的语法?,第1张

使用Spring 3 MVC将List 作为表单支持对象,正确的语法

也许这回答了你的问题:

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类:

public class Foo {    private String name;    public Foo() {    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}

JSP VIEW(名称= 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>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4907332.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存