问题
I'm trying to submit array with new items using spring webflow. For eaxmple if myList has size 3 and then i add 4th item then submit fails.
<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>
So how to submit such form?
回答1:
because the ArrayList you are providing the databinder has a predefined fixed size and cannot accept new entries.
You need to wrap your ArrayList with an AutoPopulatingList (located in spring-core.jar) before attempting to add any new entries to the data binder in your flows.... (or simply use an AutoPopulatingList on your pojo to avoid wrapper method).
Sample conversion method:
import org.springframework.util.AutoPopulatingList;
//class definition skipped
public <T> List<T> wrapListWithAutoPopulatingList(List<T> list, Class<?> pojoClazz) {
List<T> apl = new AutoPopulatingList(list, pojoClazz ) ;
return apl;
}
Java Doc:
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.
Also, please be aware of the 'autoGrowCollectionLimit' property on the initBinder. The default max value is 256 entries. This can be adjusted if you need more (or less). See
Can not post form with many (over 256) values
来源:https://stackoverflow.com/questions/30910692/spring-webflow-submit-array-with-new-items