Spring MVC映射具有相同名称的多个动态表单元素

lit*_*leK 6 javascript spring jsp spring-mvc

我有一个Spring MVC应用程序,我想知道如何成功地将我的JSP页面中具有相同名称的多个动态表单元素映射到我的对象类.例如:

在我的locations.jsp页面中,我有多个下拉框:

<form id="tabs-3-form">
    <input id="locations-1" name="location" />
    <input id="locations-2" name="location" />
    <input id="locations-3" name="location" />
    ... (more can be added or deleted dynamically by user)
</form>
Run Code Online (Sandbox Code Playgroud)

我正在使用jQuery将表单发布到我的控制器:

$("#tabs-3-form").submit(function() {
    $.ajax({
        type: 'POST',
        url: '/searchResults',
        data: $(this).serialize(),
        dataType: 'json',
        success: function(data) {
           ...
        }
    });
    return false;
});
Run Code Online (Sandbox Code Playgroud)

我的LocationsController.java设置如下:

@RequestMapping(value = "/locationResults", method = RequestMethod.POST)
public @ResponseBody LocationsCollection locationsCollection
(
    @ModelAttribute(value = "location") Location location,
    BindingResult result
) 
{   
    LocationsCollection locationsCollection = new LocationsCollection();
    locationsCollection.addLocation(location);

    // Anything else to do here?

    return locationsCollection;
}
Run Code Online (Sandbox Code Playgroud)

LocationsCollection.java只包含一个Location对象列表.

我是否需要在输入字段的名称中添加括号?MVC是否会自动映射到List,就像其他表单元素一样?如果有人能提供一个例子,我会很感激.

lit*_*leK 3

我能够通过以下示例使其正常工作:http://lifeinide.blogspot.com/2010/12/dynamic-forms-lazylist-and-transparent.html ?showComment=1355160197390#c6923871316812590644

不过,我确实做了一项调整。对于表单名称,我使用:

<input name="locationList[0].locationName" />
Run Code Online (Sandbox Code Playgroud)

而不是文章建议的内容:

<input name="myFormObject.elements[0].property" />
Run Code Online (Sandbox Code Playgroud)