File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Finding Session Object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Finding Session Object?" Watch "Finding Session Object?" New topic

Finding Session Object?

JD Thompson

Joined: Jun 29, 2006
Posts: 21
We are using Struts for a J2EE 1.4 application. On the server side, I have to initialize a Strut form's values/fields BEFORE the associated Action is called. The way the lead tech has designed the architecture, this is my challenge.

The only way I can initially see how to handle this is to load the form's values as part of a constructor for the form. However, I don't know how to reach the session object.

Is there a way to call the container and receive a collection of objects, do a search for instanceof session and then retrieve the needed ejb from there?

Thanks for your help in advance!!!


New Developer<br />Happy to learn what I don't know<br />Happy to share what I do<br />
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
first: i'm not an expert in this constellation. but as far as i recall it is NOT the case that you have one ActionForm instance per request. This is handled transparently by the Struts request processor. which obviously means that you cannot place code in constructors which do a individual setup.

also - if a form does the client server roundtrip, struts will call getters and setters BEFORE the action is called. i would rather go with the (struts) achitectural intention and work with forms from within actions.

long talk short sense - looks like a terrible bad idea in my eyes. maybe merrill has some comments too...

good luck,
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
First of all, I disagree with your lead tech's approach. ActionForm beans were designed to be simple beans that carry data to and from a page. Struts was designed so that Action classes do the actual work of manipulating the data in ActionForms.

Having said that, there is a way to accomplish what you want. ActionForm has the following method that you can overrride:

This method is called after an ActionForm is created, but before any of its setters are called if a form is being submitted. Since you are passed the request as a parameter, you can use it to get the session and do whatever you want with it. I'd suggest a lazy initialization approach where you check to see if the data is null, and if it its, initialize it.

Consultant, Sima Solutions
JD Thompson

Joined: Jun 29, 2006
Posts: 21
Jan and Merrill,

Thank you both for your thoughts and suggestions!! I agree the situation is less than ideal but you have definitely helped me out in terms of having another plan of attack (i.e. overriding the reset method).

I will let you know if success is reached!

Take care and have a great weekend!!

Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I have to wonder how much Struts development experience your lead tech has. When you say "BEFORE the associated Action" what do you view as the "associated Action"? Are you talking about initializing data before the initial page is shown or after the user clicks the submit button but before your execute method?

For a general form type page I almost always have an action that is used to display the page. The role of this action is to initialize default form values in the case of an "add" and to populate the form values in the case of an "edit". Would this work for you?

- Brent
I agree. Here's the link:
subject: Finding Session Object?
It's not a secret anymore!