• 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 can Action class will get access to HttpServletRequest to access data??

 
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Please help me understand this .I am completely new to struts2 version .

I am referring to tutorials but couldn't get this concept. Posted so that i can get help here .
How can Action class will get access to HttpServletRequest to access data??

class NewAction {
public void String execute() throws Exception {
if ( its-ok() ) {
return "login";
}
else {
return "none";
}}}


 
Ranch Hand
Posts: 1211
Mac IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Implement RequestAware interface to get access to the request attributes.
To get the actual request object, you can implement ServletRequestAware.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In general Struts 2 abstracts the request away; the framework populates action properties from request parameters. Assuming the form:the action would just define a "name" property--no need to access the request:You can access request parameters through a map by implementing ParameterAware if really necessary, but it's usually not.

You can access the HttpServletRequest itself by either implementing ServletRequestAware or using the ServletActionContext static utils. It's relatively rare to need to do either of these, and for obvious reasons it's not recommended.
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks sonny and David for your replies.

I doesn't want to extend the ActionSuppourt class to get access to the data .I want to go with the ServletRequestAware interface only.

so for this can i do directly like this ??



A single class is sufficient or do i need to use anyother class ??

 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can, but again, it's a bad idea.
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:You can, but again, it's a bad idea.



Then please tell me whats the correct approach ?? I could not understand them .

Do i need to write a separate class again for implementing this Aware interface ??
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code I provided above is the canonical (normal, usual) way to get at form/request data in Struts 2.

You don't *have* to extend ActionSupport, but you'd have to convince me there's a good reason not to--it provides the validation and localization support needed by nearly any web application.
 
Sonny Gill
Ranch Hand
Posts: 1211
Mac IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Dave is saying, things are done differently in Struts 2, and for good reasons.

I suggest that you do some more reading on Struts 2 basics.
A good start will be the tutorial and other resources at http://www.struts2.net/.
There is also a free book available at InfoQ - Starting Struts 2.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic