gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:46:45 26 4
gpt4 key购买 nike

假设我有这样的表格:

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com