Welcome to WebmasterWorld Guest from 3.228.24.192

Forum Moderators: phranque

Message Too Old, No Replies

Accessing the session from a Struts ActionForm

When the page is loaded, how can the session be accessed

     
5:23 pm on Feb 21, 2007 (gmt 0)

Full Member

10+ Year Member

joined:June 29, 2005
posts:216
votes: 0


Hey guys,

Very quick question - this must be a really common thing, I just can't figure it out. How do you gain access to the Session from within the ActionForm itself?

I have a struts application. I have a jsp page, with it's own Action and ActionForm.

Now, when a button is pressed, it fires the execute method in its Action, of which one of the parameters is an instance of HttpServletRequest, from which the session can be reached.

My problem isn't at this stage though. I need to access the session when the page is first loaded, before the user clicks on anything at all, ie, when the page is being initialised, basically to prepopulate the page.

In the ActionForm, I can preset its properties by using its default constructor, but I cannot figure out how to get to the session.

Do you have to pass it along from the previously-accessed page when you are redirecting to this page? Is there a specific method call I need to make? Or am I looking in completely the wrong place?

Any advice will be greatly appreciated - I know this can be done, and it's driving me mad, that I can't do it!

Thanks - if you need further clarification, just let me know.

B

*EDIT - Apologies if this is in the wrong forum...still can't seem to find the right one for Java issues!

5:47 pm on Feb 21, 2007 (gmt 0)

Full Member

10+ Year Member

joined:June 29, 2005
posts:216
votes: 0


Ok...solved my own problem.

A hint for anyone else in the same situation - use the ActionForm's Reset method.

public void reset(ActionMapping mapping, HttpServletRequest request)

This is run when the page is first created, and guess what, you've got access to the happy little request object there!