有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaSpringWebFlow提交包含新项的数组

我正在尝试使用SpringWebFlow提交包含新项目的数组。对于EAX示例,如果我的列表大小为3,然后我添加了第4项,则提交失败

<c:forEach items="${myList}" var="item" varStatus="status">
  <tr>
    <td>
       <input type="number" readonly class="form-control" value="${item.a}" name="myList[${status.index}].a"/>
    </td>
    <td>
      <input type="number" class="form-control" value="${item.b}" name="myList[${status.index}].b"/>
    </td>
    <td class="text-center">
      <i class="fa fa-trash delete" data-link="${flowExecutionUrl}&_eventId=deleteItem&itemId=${item.id}"></i>
    </td>
  </tr>
</c:forEach> 

<tr>
  <td>
    <input type="number" readonly class="form-control" value="1234" name="myList[3].a"/>
  </td>
  <td>
    <input type="number" class="form-control" value="5678" name="myList[3].b"/>
  </td>
  <td class="text-center">
    <i class="fa fa-trash delete"></i>
  </td>
</tr>

那么如何提交这样的表格呢


共 (1) 个答案

  1. # 1 楼答案

    因为提供databinder的ArrayList具有预定义的固定大小,因此无法接受新条目

    在尝试向流中的数据绑定器添加任何新条目之前,需要使用AutoPopulatingList(位于spring core.jar中)包装ArrayList。。。。(或者简单地在pojo上使用AutoPopulatingList以避免使用包装器方法)

    样本转换方法:

    import org.springframework.util.AutoPopulatingList;
    

    //跳过了类定义

            public <T> List<T> wrapListWithAutoPopulatingList(List<T> list, Class<?> pojoClazz)  {
    
                List<T> apl = new AutoPopulatingList(list, pojoClazz ) ;
                return apl;
            }
    

    Java文档:

    Simple List wrapper class that allows for elements to be automatically populated as they are requested. This is particularly useful for data binding to Lists, allowing for elements to be created and added to the List in a "just in time" fashion.

    Note: This class is not thread-safe. To create a thread-safe version, use the java.util.Collections.synchronizedList utility methods.

    Inspired by LazyList from Commons Collections.

    另外,请注意initBinder上的“autoGrowCollectionLimit”属性。默认最大值为256个条目。如果您需要更多(或更少),可以对其进行调整。看

    Can not post form with many (over 256) values