In Struts2, a controller dispatches a Request to an Action and the Action passes it to 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 Struts2 ? Definitely it's not View... is it the Controller or Model?
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.It's definitely (part of) the Controller, but it's also (part of) the Model.
My 2 cents:
The Action is the Controller because...
...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.
The Action is the Model because...
... Struts2 is a Pull-MVC framework:
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 throughgetSomething()
getter methods.And now, the important thing: do NOT use ModelDriven :o)
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.
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.
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.