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

邮差的信 提交于 2019-12-23 15:49:17

问题


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?


回答1:


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.




回答2:


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:

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)




回答3:


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!


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.




来源:https://stackoverflow.com/questions/36578574/what-is-action-used-in-struts-2-in-terms-of-mvc-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!