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

爱⌒轻易说出口 提交于 2019-12-25 02:46:12

问题


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"/>

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



回答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.



来源:https://stackoverflow.com/questions/20439106/how-to-post-an-array-of-custom-objects-with-missing-entries-to-struts2-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!