简体   繁体   English

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

[英]Spring MVC map multiple dynamic form elements with the same name

I have a Spring MVC application and I am wondering how to successfully map multiple, dynamic form elements with the same name in my JSP page to my object class. 我有一个Spring MVC应用程序,我想知道如何将JSP页面中具有相同名称的多个动态表单元素成功映射到我的对象类。 For example: 例如:

In my locations.jsp page, I have multiple dropdown boxes: 在我的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>

I'm using jQuery to POST the form to my controller: 我正在使用jQuery将表单过帐到我的控制器:

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

My LocationsController.java is set up as follows: 我的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;
}

LocationsCollection.java just contains a List of Location objects. LocationsCollection.java仅包含一个Location对象列表。

Do I need to add brackets to the names of my input fields? 我是否需要在输入字段的名称中添加方括号? Will MVC automatically do the mapping to a List, as it does with the other form elements? MVC是否会像其他表单元素一样自动进行到列表的映射? If anyone could provide an example, I'd appreciate it. 如果有人可以提供示例,我将不胜感激。

I was able to get it working by following the example from: http://lifeinide.blogspot.com/2010/12/dynamic-forms-lazylist-and-transparent.html?showComment=1355160197390#c6923871316812590644 我可以通过以下示例操作它: http : //lifeinide.blogspot.com/2010/12/dynamic-forms-lazylist-and-transparent.html?showComment=1355160197390#c6923871316812590644

I did make one adjustment, however. 但是,我确实做了一个调整。 For the form names, I used: 对于表单名称,我使用了:

<input name="locationList[0].locationName" />

instead of what the article suggests: 而不是文章建议的内容:

<input name="myFormObject.elements[0].property" />

I think you need to use brackets if you are using same name for form elements. 我认为如果表单元素使用相同的名称,则需要使用方括号。

<c:forEach items="${expenseForm.expenseDetails}" varStatus="i">
  <tr id="expenseDetail_${i.index}" class="lineItemsClass" lineNum="${i.index}">
    <td><form:input path="expenseDetails[${i.index}].description" cssStyle="width: 200px;" /> <label style="color: red;"><form:errors path="expenseDetails[${i.index}].description" /></label></td>
    <td><form:input path="expenseDetails[${i.index}].quantity" cssStyle="width: 60px;" readonly="true"/> <label style="color: red;"><form:errors path="expenseDetails[${i.index}].quantity" /></label></td>
    <td><form:input path="expenseDetails[${i.index}].unitPrice" cssStyle="width: 60px;" readonly="true"/> <label style="color: red;"><form:errors path="expenseDetails[${i.index}].unitPrice" /></label></td>
    <td><form:input path="expenseDetails[${i.index}].total" cssStyle="width: 60px;" /> <label style="color: red;"><form:errors path="expenseDetails[${i.index}].total" /></label></td>
  </tr>
</c:forEach>

Here expenseDetails is the list in the modelAttribute class. 这里的expenseDetails是modelAttribute类中的列表。 The path name will be used as html form name and it will be indexed based. 路径名将用作html表单名,并将基于索引创建。 The above code segment is working fine for me. 上面的代码段对我来说很好用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM