List< Foo>作为使用Spring 3 MVC的表单支持对象,正确的语法? [英] List<Foo> as form backing object using Spring 3 MVC, correct syntax?

查看:112
本文介绍了List< 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) {
    // ...
}

推荐答案

也许这是您的问题:

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>

这篇关于List&lt; Foo&gt;作为使用Spring 3 MVC的表单支持对象,正确的语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆