• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this code

(Not sure how your class named KeyValuePair looks, so no guarantees that this will work, maybe David has a better solution)...
 
Peter Searls
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic