This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Struts and the fly likes S2: Actions are not Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "S2: Actions are not Servlet" Watch "S2: Actions are not Servlet" New topic
Author

S2: Actions are not Servlet

Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Struts 2 action classes are most certainly not servlets, nor particularly similar.

This info forced me to dig up Struts 2 site. Here I come up with one point.

"S2 has Action classes which are extended by ActionSupport (or Action interface) classes, this is not the general requirement, you can access any Java class with execute() method, Struts will find it for you."

Am I right ?

And it means there is no HttpServletResponse/Request objects, nor any J2EE "javax.servlet.http" classes used/implemented in S2?

[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Joachim Rohde
Ranch Hand

Joined: Nov 27, 2006
Posts: 423

You *can* extend from ActionSupport but it's not a *must*. If you have an execute-method in your action it will be called automatically.
Regarding the HTTP*-Objects have a look at the Servlet Config Interceptor (http://struts.apache.org/2.0.14/docs/servlet-config-interceptor.html). You have access to those objects but you need to implement certain interfaces so Struts can inject those objects.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Are you right that the S2 site says that, or is it correct that S2 actions don't *have* to extend ActionSupport? I don't know if the S2 site says that, but it's true that actions don't *have* to extend ActionSupport.

That has nothing to do with the second part of your question, though. One of the main points of S2 is to abstract itself away from the servlet spec--one of the *major* pain points with Struts 1 (that's why WebWork went that route).

As the previous commenter states you *can* get access to the servlet spec objects, but there are very few situations where that's necessary. Since there are trivial, non-servlet-spec-related ways to get the attribute/parameter maps of the servlet spec objects, one more use case for accessing servlet spec objects is eliminated.

All of that combines to make action testing far, *far* easier.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Thanks David & Joachim

David Newton wrote:I don't know if the S2 site says that, but it's true that actions don't *have* to extend ActionSupport.

Agreed, And that perticualr site/link is this and this the tip form the same link
Implementing the Action interface is optional. If Action is not implemented, the framework will use reflection to look for an execute method.

David Newton wrote:
One of the main points of S2 is to abstract itself away from the servlet spec--one of the *major* pain points with Struts 1 (that's why WebWork went that route).

hmm, that's something new to me, I didn't imagine an Java web framework without servlet spec. So it means they created all new spec for web application from scratch?
David Newton wrote:
All of that combines to make action testing far, *far* easier.

Yeah, it looks from the Action code, which has a plain "execute()"method and not depending upon any (http) parameters or states.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

So it means they created all new spec for web application from scratch?

No, it means the servlet spec is abstracted away from the application code.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

If that so, can I really start developing Web application using S2 without any prior knowledge of Servlets ? After all, S2 dosen't really used those "javax.http.servlet" package..
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Yes, you can, although when something *does* go wrong you may be less likely to understand why. It's still very important to understand the underlying processes and mechanisms.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

OK, Thanks David
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: S2: Actions are not Servlet