Reputation: 626
In Struts2, a Controller dispatches a request to the Action and then passes it to the back-end logic, which could be regarded as a very big model, to process the request, and JSP represents Views.
How to define the Action in Struts 2? Definitely it's not a View. Is it the Controller or Model?
Upvotes: 3
Views: 1540
Reputation: 1
The action is definitely close to the terms as controller rather than model. Especially if you use REST with Struts2 you can read Mapping REST URLs to Struts 2 Actions.
Actions or Controllers? Most Struts 2 developers are familiar with the Action. They are the things that get executed by the incoming requests. In the context of the REST plugin, just to keep you on your toes, we'll adopt the RESTful lingo and refer to our Actions as Controllers. Don't be confused; it's just a name!
If you need to dismiss a confusion about MVC pattern used for the architecture of Struts 2 framework, then you can read Confusion in Struts 2 MVC architecture:
Actually Struts2
Action
s are controller delegates. And Struts2 provides avalueStack
on the View layer which has anAction
on top of it, and if you want to use a pseudo-model then action should implementModelDriven
interface.You should also note that Struts2 actions are simple POJOs managed by Struts2 container. This is a bit different in the MVC point of view, also known as MVC Model2.
The controller is responsible to handle the request and return a view as a result. That is what the action is doing in Strust2.
The fact that users frequently aggregate their models with the controller doesn't misplace the controller definition. Then if a controller has a model then you can think it's a part of the big model. It is not.
The never the less important part is the communication of the model and view. In Struts2 it's performed via the action context. The view should have access to the action context to retrieve the model. This is wired by the OGNL.
The framework sets the OGNL context to be our
ActionContext
, and the value stack to be the OGNL root object.
In the current version of Struts the action/controller is pushed to the value stack, and accessed the same way like a model. It's harmless operation because controllers are thread-safe instances. Why not to reuse them like models?
It is also harmless to aggregate a model objects to the controller and access them from there. You can associate any number of models to the same action. But if you think about one model, then you can use ModelDriven
action. But the last one is not recommended because it brings unnecessary complexity to architecture of Struts2 application, and unfortunately error-prone.
Upvotes: 1
Reputation: 2104
Struts actions are controllers in the sense of the MVC pattern. I think the discussion of the value stack and ActionContext
, as well as getter methods in action classes confuses the issue. In general, these are simply containers for other objects (usually Model objects).
While @AndreaLigios points out that you can retrieve objects from the action using various get methods, that's more an issue of diluting the actions cohesion by giving it additional responsibilities normally assigned to model object. Yes, it's important to evaluate the responsibilities of your objects when you're considering what the do (or should be doing).
Put most simply, the responsibilities of the major components in all MVC framework are as follows:
When you look at a specific MVC framework like Struts (or Spring MVC) you'll see that the frameworks usually provide both Controller and View components, but it's your job to build out the Model yourself. Even so, Struts provides a wealth of additional objects and components, like ActionContext
, that make it easier to access your Model objects from your View components.
Upvotes: 1
Reputation: 50261
It's definitely (part of) the Controller, but it's also (part of) the Model.
My 2 cents:
...in Struts2 the Controller is composed by everything responsible of reading, interpreting and manipulating the request, in order to propose an appropriate response, and hence yes, the Action is the controller, along with every Interceptor in the Interceptor Stack, and we could widen the meaning to include the Filter, the ActionMapper and so on.
... Struts2 is a Pull-MVC framework:
Pull-MVC and Push-MVC are better understood with how the view layer is getting data i.e. Model to render. In case of Push-MVC the data( Model) is constructed and given to the view layer by the Controllers by putting it in the scoped variables like request or session. Typical example is Spring MVC and Struts1. Pull-MVC on the other hand puts the model data typically constructed in Controllers are kept in a common place i.e. in actions, which then gets rendered by view layer. Struts2 is a Pull-MVC based architecture, in which all data is stored in Value Stack and retrieved by view layer for rendering.
Then in Struts2 the Value Stack is the Model, and the Action is pushed on (top of) the ValueStack. Hence, the Action is part of the model. We could widen the meaning of Model to include the whole ActionContext (with all its scopes - Page, Request, Session... )
Then the Action is the controller when inside your execute()
method, and is the model when it stores the attributes that you fetch from the JSP through getSomething()
getter methods.
And now, the important thing: do NOT use ModelDriven :o)
Upvotes: 0