This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

nested beans - setting values in form

 
Klaus Jesper
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hallo,
I have a little problem when using complex forms with struts.

I have a form with is a quite complex table. To populate it, I use some nested Beans (Bean1 has a collection of Bean2, Bean2 has a collection of Bean3, Bean3 has a collection...). Each Bean has a name (simplified!) and a collection of a "sub-Bean".
In the jsp I access the data with:



The example above deals with <bean:write> but I need to use <html:text> to make the data changeable. Should be no big deal so far (with a little help of struts-el/jstl).

But how could I submit changed values? The form expects a getter/setter method for each value(?). How should it look for example for getting data from test3_id.name into my form?

Maybe I am on the wrong way with my nested Beans? Would be nice to have some comments on that..

Klaus
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Struts supports nested properties in ActionForms.

So, if form.getObject1().getObject2().getProperty1() ... then, you can have


Etc ....
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nathaniel is correct but I think Klaus's scenario is a little more complicated. I imagine that there is a dynamic number of collections inside a dynamic number of collections... etc.

This topic doesn't get tackled much. I'm glad you brought it up. I will share my solution and I'd like to hear what others have to share.

I use map-backed forms and cheat. The String "key" that interacts with the map becomes a listing of the index numbers. For example, let's say I want to reach the 5th object of the 3rd object of the 2nd object of the main list, the key is "2-3-5" (I used "-" to tokenize the indexes but any non-numeric character could be used).

In your Action you would parse out the keys and put the submitted values in their respective objects. You will also need to create a Setup Action to convert your objects into the map format for prepopulating the values into your jsp.
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Klaus Jesper:
But how could I submit changed values? The form expects a getter/setter method for each value(?). How should it look for example for getting data from test3_id.name into my form?

Maybe I am on the wrong way with my nested Beans? Would be nice to have some comments on that..


What you want to do is make use of the Struts <nested> taglib. This taglib should accomplish exactly what you are trying to do. Check out this link for more info. You'll want to read the sections labeled [NeXT] and PilotLight.
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marc Peabody:
I use map-backed forms and cheat. The String "key" that interacts with the map becomes a listing of the index numbers. For example, let's say I want to reach the 5th object of the 3rd object of the 2nd object of the main list, the key is "2-3-5" (I used "-" to tokenize the indexes but any non-numeric character could be used).

In your Action you would parse out the keys and put the submitted values in their respective objects. You will also need to create a Setup Action to convert your objects into the map format for prepopulating the values into your jsp.


I'm not following you Marc. I'm not understanding why you would have to go through all this. I've had similar scenarios and simply used the nested taglib with no problems. I'd be interested in hearing where you're going with this. Might be worth another topic if you think it would be hijacking Klaus' too much.
[ January 21, 2005: Message edited by: Jason Menard ]
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is far from hijacking. I have to explain my shananigans to Klaus now.

I didn't realize the power of the nested taglib because I'd never used it. I thought the primary purpose was for nesting logic tags. Boy was I off! That great link you posted showed me that the primary purpose was for nesting beans.

My example was a way to get around the problem of Struts not providing "all the logic necessary to allow the nesting of beans" without recognizing the nested taglib.

Thanks again for the link Jason.
 
Klaus Jesper
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answers!

I'll take a close look on the page suggested by Jason today and than try this whole nested-thing.

Klaus
[ January 22, 2005: Message edited by: Klaus Jesper ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic