如何将表单字段集合发布到Spring控制器?

Car*_*ark 2 java spring spring-mvc

假设我有这样的形式:

<form method="post" action="/create">

    <input type="text" name="title.0" value="Curious George" />
    <input type="text" name="author.0" value="H.A. Rey" />
    <input type="text" name="date.0" value="2/23/1973" />

    <input type="text" name="title.1" value="Code Complete" />
    <input type="text" name="author.1" value="Steve McConnell" />
    <input type="text" name="date.1" value="6/9/2004" />

    <input type="text" name="title.2" value="The Two Towers" />
    <input type="text" name="author.2" value="JRR Tolkien" />
    <input type="text" name="date.2" value="6/1/2005" />

    <input type="submit" />
</form>
Run Code Online (Sandbox Code Playgroud)

我如何从Spring MVC 3.0控制器解析这个?

Boz*_*zho 8

name属性不必是唯一的.所以:

<input type="text" name="title" value="Curious George" />
<input type="text" name="title" value="Code Complete" />
<input type="text" name="title" value="The Two Towers" />
Run Code Online (Sandbox Code Playgroud)

然后

@RequestMapping("/create")
public void create(
    @RequestParam("title") List<String> titles, 
    @RequestParam("author") List<String> authors, ..) {..}
Run Code Online (Sandbox Code Playgroud)

根据规范,应保留元素的顺序:

控件名称/值按它们在文档中出现的顺序列出.名称通过'='与值分隔,名称/值对通过'&'彼此分隔.