Dynamic List Position Index with Spring Form

For decorated unordered lists:

private List<MyListItem> items = LazyList.decorate(new ArrayList(), FactoryUtils.instantiateFactory(MyListItem.class)); 

Is it mandatory to specify attributes in the form with an index number? eg:

 <form:input path="items[1]" /> <form:input path="items[2]" /> 

Why can't I provide two brackets, for example in PHP?

 item[] 

Since dynamically creating an input list with the DOM will be a problem to eliminate the removal of items ...

+4
source share
2 answers

Similarly, you ask why Spring does not support a generic template type, such as item [], you can also ask why your collection is not ordered according to the order of the elements displayed by your form. Keep in mind: java.util.List is an ordered collection, so you should tell Spring where every item in the list should be inserted.

Bypass

option 1ΒΊ

Create an AutoPopulatingList as follows

 private List<Item> items = new AutoPopulatingList( new ElementFactory() { public Object createElement(int index) throws ElementInstantiationException { /** * Any removed item will be handled as null. * So we just remove any nullable item before adding to our List * By using the following statement */ items.removeAll(Collections.singletonList(null)); return new Item(); } }); 

option 2ΒΊ

Since Spring is open source, you can create a custom BeanWrapperImpl . Behind the scenes, BeanWrapperImpl is responsible for filling out your bean. Then compile your own Spring MVC

+1
source

You can access your variables, for example:

 <form:input path="${item[0]}" /> <form:input path="${item[1]}" /> 

Another way:

 <c:forEach items="${items}" var="item"> <form:input path="${item}" /> </c:forEach> 
-1
source

All Articles