您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

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

也许这回答了你的问题:

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>
Java 2022/1/1 18:19:04 有551人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶