This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to access the request object in struts2 action

 
Marc Heruela
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Marc Heruela
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic