• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to access the request object in struts2 action

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic