Search code examples
model-view-controllerdesign-patternsarchitecturestruts2struts

What is Action used in Struts 2 in terms of MVC pattern?


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 a View. Is it the Controller or Model?


Solution

  • 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:

    • Model objects are responsible for holding data gathered or calculated within your application domain.
    • View objects are responsible for displaying information to users, or other recipients (like service clients)
    • Controller objects are responsible for coordinating the flow of data among the model and view components.

    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.