使用spring 3 mvc列出<foo>作为表单后备对象,正确的语法?</foo>

时间:2012-03-08 11:50:25

标签: spring jsp spring-mvc jstl

我想做类似这样的事情,其中​​foo是一个带有一个String字段名的类,以及getter / setter:

<form:form id="frmFoo" modelAttribute="foos">
   <c:forEach items="${foos}" var="foo">
     <form:input path="${foo.name}" type="text"/>

然后提交更新名称的Foos完整列表?我的控制器sig看起来像这样:

@RequestMapping(value = "/FOO", method = RequestMethod.POST)
public String getSendEmail(List<Foo> foos, Model model)
{}

2 个答案:

答案 0 :(得分:33)

也许这回答了你的问题:

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>

答案 1 :(得分:0)

虽然上面的答案有效,但这里有一个替代方案,不需要你创建一个包装类/表单类。

模型和控制器

public class Foo {
    private String name;
    private List<Foo> fooList; //**must create this list, also getter and setter**
    public Foo() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public List getFooList() {
        return fooList;
    }

    public void setFooList(String fooList) {
        this.fooList = fooList;
    }

}

@Controller("/")
public class FooController{

    //returns the ModelAttribute fooListWrapper with the view fooForm
    @RequestMapping(value = "/FOO", method = RequestMethod.GET)
    public String getFooList(Model model) {
        List<Foo> fooList = service.getFooList();

        model.addAttribute("fooList", fooList);

        return "list_foo"; //name of the view
    }

    @RequestMapping(value = "/FOO", method = RequestMethod.POST)
    public String postFooList(@ModelAttribute("foo")Foo foo, Model model) {
        List<Foo> list = foo.getFooList(); // **This is your desired object. 
        //If you debug this code, you can easily find this is the list of  
        //all the foo objects that you wanted, provided you pass them properly. 
        //Check the jsp file to see one of the ways of passing such a list of objects**
        //Rest of the code
    }

}

JSP视图

<form:form id="form" action="<paste-target-url-here>" method="POST" modelAttribute="fooList">


    <c:forEach items="${fooList}" varStatus="i">
           <form:input path="fooList[${i.index}].name" type="text"/>
           <!-- Here you are setting the data in the appropriate index which will be caught in the controller -->
    </c:forEach>


    <button>submit</button>
</form:form>
相关问题