Struts 2 - Problem updating textfields created by iterating over a list of objects
Joined: Dec 21, 2009
I iterated over a list of KeyValuePair objects (get/set for properties key and value).
This list was initialized as
List<KeyValuePair> kvp = new ArrayList<KeyValuePair>();
The list was retrieved from a page bean named "tb" returned from the action. The "tb" obejct has a getKvp() : List<KeyValuePair> method.
Resulting in two groups of text fields. A key textfield and a value textfield followed by a ruled line then another group of key textfield and value textfield. All four fields were properly initialized.
On the page, I changed the value in the first key textfield from "aaa" to "ccc" and the first value field from "111" to "333" and submitted the form. When the List of KeyValuePair objects was added to the "tb" page bean and stored in the action, there were four KeyValuePair objects in the list. when printed out we see:
Key: "ccc" Value: null
Key: "bbb" Value: null
Key: null Value: "333"
Key: null Value: "222"
It is creating one KeyValuePair object for each textfield displayed even though OGNL originally created the displayed output with the values for two textfields from each KeyValuePair object.
Does anyone know how to group each key and value textfield combination together so that both values get added to one KeyValuePair object instead of one per KeyValuePair?
Thank you for the suggestion, unfortunately it didn't work. I received OgnlExceptions saying that the target is null for setProperty.
It seems that Ognl isn't seeing that the first set of Key / Value textfields are related and should go into one object. I'm not sure that there is any way to make it see that. Obviously in this short example I could replace KeyValuePair with an Array of Array of Strings and everything would be ok. But this is just a test, my actual requirement is to use an object containing 15 or 20 properties.
Here is the code for the KeyValuePair.
Joined: Dec 21, 2009
The problem has been solved.
As you can see below the iterator goes through all of the entries in the "tb.kvp" list. However for the textfields, I go get the "tb.kvp" list again and reference the specific entry in the list based upon where the iterator is. Now OGNL can put it together that these two fields are part of the same object, not separate objects.