spring:一个POST中的多个参数

时间:2010-11-23 20:09:05

标签: spring forms post spring-mvc

您好我正在使用Spring MVC 3注释。

如何在一个 Spring ModelAttributes 标记中使用多个<form:form>

使用一个ModelAttribute完全可行,但嵌套它们并使用两个不起作用。有没有办法做到这一点?或者我如何在同一 POST 请求中发送 ModelAttribute RequestParameter

1 个答案:

答案 0 :(得分:3)

您可以使用holder对象轻松地将多个对象组合到一个模型属性中,如下所示:

public class Foo {
    private String name;
    ...
}

public class Bar {
    private String name;
    ...
}

public class FooBarForm {
    private Foo foo;
    private Bar bar;
    ...
}

<form:form modelAttriubte = "fooBarForm" method = "post">
    <form:input path = "foo.name" />
    <form:input path = "bar.name" />
</form:form>

@RequestMapping(...)
public String processForm(@ModelAttribute FooBarForm form, ...) { ... }

@RequestParam也可以与@ModelAttribute结合使用(请注意,Spring标记不支持@RequestParam):

<form:form modelAttriubte = "foo" method = "post">
    <form:input path = "name" />
    <input type = "text" name = "bar_name" />
</form:form>

@RequestMapping(...)
public String processForm(@ModelAttribute Foo foo, 
    @RequestParam("bar_name") String barName) { ... }