Skip to content Skip to sidebar Skip to footer

How To Post An Array Of Custom Objects With Missing Entries To Struts2 Action

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

Solution 1:

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");

Solution 2:

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.


Post a Comment for "How To Post An Array Of Custom Objects With Missing Entries To Struts2 Action"