How to post an array of custom objects with missing entries to Struts2 action

821 views Asked by At

Tabular Inputs This suggests to use XWorkList instead of ArrayList, when the size of array is unknown & there are gaps in between.

But XWorkList is not generic & it has no empty constructor, according to documentation.

My question is how to use XWorkList or is there any way to submit list of beans with some items missing in the list ?

Sample Html:

<input name="lst[0].name"/>
<input name="lst[3].name"/>
<input name="lst[4].name"/>
2

There are 2 answers

2
Roman C On

A List contract is an ordered collection. It can't have missing indeces. About XWorkList it's just an other implementation of the list, capable of creating new elements for the specified index by filling gap for required elements. What is said for add(int, Object)

Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). This method is guaranteed to work since it will create empty beans to fill the gap between the current list size and the requested index to enable the element to be set.

Another approach for creating new beans in the list is to use type conversion. Or using annotations like in this answer.

0
t0mppa On

It's extending ArrayList and has a constructor that takes a Class. Thus you would change:

List<String> foo = new ArrayList<String>();
foo.add("bar");
foo.add("");
foo.add("");
foo.add("foobar");
foo.add("barfoo");

into:

List<String> foo = new XWorkList(String.class);
foo.add("bar");
foo.add(3, "foobar");
foo.add(4, "barfoo");