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

发布于 2024-11-03 01:13:18 字数 806 浏览 1 评论 0原文

假设我有这样的表单:

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

How do I parse this from a Spring MVC 3.0 controller?

Suppose I have form such as this:

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

How do I parse this from a Spring MVC 3.0 controller?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

染柒℉ 2024-11-10 01:13:18

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 name attribute need not be unique. So:

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

And then

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

The order of the elements should be preserved, according to 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 '&'.

倾`听者〃 2024-11-10 01:13:18

如果您可以更改视图,理想情况下您可以使用某种列表来执行此操作。

类似于:

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

您将有一个包含 3 个元素的 Book 类。
和一个包含书籍列表的包含类 BookContainer

public class BookContainer {
  private List <Book> books = new ArrayList<Book>();

  public List<Book> getBooks() {
    return books;
  }

  public void setBooks(List<Book> books) {
    this.books = books;
  }
}

现在在你的控制器中,你将有一个 @ModelAttribute 方法,它返回要绑定的 Containing 类:

@ModelAttribute("container")
public BookContainer getBookContainer() {
  return new BookContainer;
}

最后你您的请求映射方法有一个 @ModelAttribute 参数:

@RequestMapping
public void handlePost(@ModelAttribute("container") BookContainer container) {

}

spring 会根据您的需要自动将尽可能多的“书籍”添加到您的列表中。

If you can change the view, ideally you'd do this with some kind of list.

Something like:

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

you would have a Book class containing your 3 elements.
and a containing class which contains a list of books BookContainer

public class BookContainer {
  private List <Book> books = new ArrayList<Book>();

  public List<Book> getBooks() {
    return books;
  }

  public void setBooks(List<Book> books) {
    this.books = books;
  }
}

Now in your controller, you'd have a @ModelAttribute method which returns the Containing class to to bind to:

@ModelAttribute("container")
public BookContainer getBookContainer() {
  return new BookContainer;
}

finally you'd have a @ModelAttribute parameter to your request mapping method:

@RequestMapping
public void handlePost(@ModelAttribute("container") BookContainer container) {

}

spring will automatically add as many 'Book's to your list as you need.

梦初启 2024-11-10 01:13:18

您的控制器请求映射可以简单地采用 spring WebRequest 作为参数,然后执行如下操作:

Map<String, String[]> params = request.getParameterMap();
int i = 0;
while ( true ) {
    String title = params.get( "title" + .i );
    if ( title != null ) {
        // get the rest and create your Book object or whatever
        i += 1;
    }
    else {
        break;
    }
}    

Could your controller request mapping simply take a spring WebRequest as the parameter and then do something like:

Map<String, String[]> params = request.getParameterMap();
int i = 0;
while ( true ) {
    String title = params.get( "title" + .i );
    if ( title != null ) {
        // get the rest and create your Book object or whatever
        i += 1;
    }
    else {
        break;
    }
}    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文