This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Struts and the fly likes Populate multiple pojo's. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Populate multiple pojo Watch "Populate multiple pojo New topic
Author

Populate multiple pojo's.

Shane Hanswyk
Greenhorn

Joined: Jan 12, 2010
Posts: 6

hi All,

I am using Struts 2 to perform servlet processing but am having trouble determining how to populate multiple pojo's with the parameter values.

My Action class looks as follows:



Address and Person are pojo's.

I would like to populate the fields in the Address and Person pojo's with the parameter values passed to this action.

I know that I can implement ModelDriven to return (and populate) address but of course, nothing will be populated into person.

I am also aware that I can implement ParameterAware which gives me a Map of parameters but I assume I would then have to individally populate the member fields of each pojo. Is there any easier way and/or am I missing something? My jsp pages are not using struts tags.

Any help would be grateful.

thanks.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Just name your parameters appropriately; address.addr1, address.state, person.firstName, whatever.
Shane Hanswyk
Greenhorn

Joined: Jan 12, 2010
Posts: 6
Thanks David for your reply.

I don't have control over the jsp. Is there any way of doing it in the Action?

ie. Maybe an intercept somewhere??
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

There's a property mapping interceptor somewhere, um... "alias" interceptor.

Another less "mysterious" option would be to create action properties for what you *are* getting and do the conversion in the action. I'm a big fan of interceptors, but quit frankly, not a huge fan of things like the "alias" interceptor.
Shane Hanswyk
Greenhorn

Joined: Jan 12, 2010
Posts: 6
Thanks again David.

I was hoping for a solution that wouldn't involve me referencing the properties individually. It appears this is not possible.

I think the best option is to create the action properties and do the conversion - as you suggested.

cheers.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It's possible, using the "alias" interceptor.
Shane Hanswyk
Greenhorn

Joined: Jan 12, 2010
Posts: 6

hmmm....interesting.

How would I go about coding this or, can you point me to somewhere that has this documented. I couldn't find any good examples.

Thanks.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Did you checkout the apache's documentation??


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Shane Hanswyk
Greenhorn

Joined: Jan 12, 2010
Posts: 6

Yes. That does not explain how to do it without "referencing every property individually".
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The "alias" interceptor docs explain how to set up the mapping. Yes, you'd (most likely--check the underlying implementation to see if there are shortcuts available) need to map each property separately in the interceptor config for that action.

I still don't think it's worth it, though.
Shane Hanswyk
Greenhorn

Joined: Jan 12, 2010
Posts: 6
I agree.

Thanks for all your help, David.
 
wood burning stoves
 
subject: Populate multiple pojo's.