gpt4 book ai didi

java - spring webflow提交带有新项目的数组

转载 作者:行者123 更新时间:2023-12-01 11:24:50 25 4
gpt4 key购买 nike

我正在尝试使用 spring webflow 提交包含新项目的数组。例如,如果列表的大小为 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>

那么如何提交这样的表单呢?

最佳答案

因为您为数据绑定(bind)器提供的 ArrayList 具有预定义的固定大小,并且无法接受新条目。

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

转换方法示例:

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

关于java - spring webflow提交带有新项目的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30910692/

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