aspose file tools*
The moose likes Struts and the fly likes How to access the request object in struts2 action Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to access the request object in struts2 action" Watch "How to access the request object in struts2 action" New topic
Author

How to access the request object in struts2 action

Marc Heruela
Ranch Hand

Joined: Jul 23, 2009
Posts: 38
This might not be a good practice, or Im not sure, but may I know how to access
the request object on the action class' constructor?

It seems ServletActionContext.getRequest() returns null.

Anybody?

Any help/explanation is much appreciated!

-marckun


-marckun
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

Your action can implement ServletRequestAware interface to get access to the request object. But I'm afraid the request object will be injected after your constructor runs. Why do you need the request object in the constructor?? Struts 2 actions are not reused, so whatever you want to do in the constructor you can also do in your execute (or whatever named) method...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Marc Heruela
Ranch Hand

Joined: Jul 23, 2009
Posts: 38
i already found a workaround.

but to answer your question sir. here is why i wanna access the request object in the constructor.

if i go to a certain page, say page A, using link_1, i will put default values on the fields (many fields) of the form on page A, otherwise, i will let it be null.

so on my action, i manually invoke the setters of the action. but i have to check which link i came from. that is why i want to initialize a flag on the constructor.

thanks sir.

-marckun
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

If you want to do some initialization, I think the best way is to implement Preparable interface so that the prepare interceptor will call the prepare method of your action class. The prepare interceptor is below the servlet config interceptor in the default stack so if your action implements both Preparable interface and ServletRequestAware interface, the HttpServletRequest object would be injected into your action before the prepare method is called...
Marc Heruela
Ranch Hand

Joined: Jul 23, 2009
Posts: 38
Now there's one neat suggestion. haha im so stupid i never thought of that (my mind is pretty beaten already.. thanks)

Thanks a lot.

Much appreciated.

-marckun
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to access the request object in struts2 action