java - 如何将表单字段集合发布到 Spring Controller ?

标签 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>

如何从 Spring MVC 3.0 Controller 解析它?

最佳答案

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" />

然后

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

应保留元素的顺序,根据 the spec :

The control names/values are listed in the order they appear in the document. The name is separated from the value by '=' and name/value pairs are separated from each other by '&'.

关于java - 如何将表单字段集合发布到 Spring Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5783291/

相关文章:

java - 如何将自定义执行器注入(inject)到播放应用程序中?

java - 如何使用 Spring MVC 从表单中的组合中获取属性值

java - Spring MVC @Controller 的处理程序方法的参数如何在运行时自动解析?

java - Spring Autowiring 在新的java对象中

java - 将 10 个文件与模板文件进行比较

java - CXF RESTful : No message body reader has been found for request class, 内容类型:application/json

java - 如何获得固定大小的队列

java - 如何在 Spring Roo/Dojo 中做一个非强制性的下拉框?

java - Hibernate实体没有被持久化

java - 在 spring 配置中设置 Object Mapper SerializationFeature