aspose file tools*
The moose likes Struts and the fly likes Struts 2 - Problem updating textfields created by iterating over a list of objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts 2 - Problem updating textfields created by iterating over a list of objects " Watch "Struts 2 - Problem updating textfields created by iterating over a list of objects " New topic
Author

Struts 2 - Problem updating textfields created by iterating over a list of objects

Peter Searls
Greenhorn

Joined: Dec 21, 2009
Posts: 9
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>();
kvps.add(new KeyValuePair("aaa","111");
kvps.add(new KeyValuePair("bbb","222");

The list was retrieved from a page bean named "tb" returned from the action. The "tb" obejct has a getKvp() : List<KeyValuePair> method.

Displayed output in jsp using:
<s:iterator value="tb.kvp" status="stat">
<b>Key: </b><s:textfield name="key" value="%{key}" /><br>
<b>Value: </b><s:textfield name="value" value="%{value}" /><br>
<hr>
</s:iterator>

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?

any help would be greatly appreciated.

Thanks,

Pete Searls


Peter Searls
psearls@hollandamerica.com
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Try this code

(Not sure how your class named KeyValuePair looks, so no guarantees that this will work, maybe David has a better solution)...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Peter Searls
Greenhorn

Joined: Dec 21, 2009
Posts: 9
Hi Ankit,
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.
Peter Searls
Greenhorn

Joined: Dec 21, 2009
Posts: 9
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.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Struts 2 - Problem updating textfields created by iterating over a list of objects