aspose file tools*
The moose likes Struts and the fly likes nested beans - setting values in form Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "nested beans - setting values in form" Watch "nested beans - setting values in form" New topic
Author

nested beans - setting values in form

Klaus Jesper
Greenhorn

Joined: Jan 20, 2005
Posts: 15
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

Joined: May 29, 2003
Posts: 1258
Struts supports nested properties in ActionForms.

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


Etc ....


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

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.


A good workman is known by his tools.
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
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

Joined: Nov 09, 2000
Posts: 6450
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

Joined: Feb 05, 2003
Posts: 4727

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

Joined: Jan 20, 2005
Posts: 15
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: nested beans - setting values in form