File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can Action class will get access to HttpServletRequest to access data??

 
RaviNada Kiran
Ranch Hand
Posts: 528
  • 0
  • Mark post as helpful
  • send pies
  • 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";
}}}


 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can, but again, it's a bad idea.
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • 0
  • Mark post as helpful
  • send pies
  • 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • 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
IntelliJ IDE Mac
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic