Java Struts 中的控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1666835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Controller in Struts
提问by ajay
What is Controller in MVC ?
MVC中的控制器是什么?
Is it struts.xml
or Servlet
(Action Class)?
是struts.xml
还是Servlet
(动作类)?
Can we have more than one Controller
in our application?
Controller
我们的应用程序中可以有多个吗?
Is it good practice to have more than one controller?
拥有多个控制器是一种好习惯吗?
回答by jitter
The ActionServlet
is the controller IMO.
这ActionServlet
是控制器 IMO。
In a broader scope the ActionServlet
together with the RequestProcessor
and Action
and the initialization info from struts-config.xml
could be called the controller in a struts app.
在更广泛的范围内,可以在 struts 应用程序中将其ActionServlet
与RequestProcessor
和Action
以及来自的初始化信息struts-config.xml
一起称为控制器。
回答by cetnar
In Struts, application Controller layer code/functionality is split into two parts:
在 Struts 中,应用程序控制器层代码/功能分为两部分:
ActionServlet
withRequestHandler
Action
classes
ActionServlet
和RequestHandler
Action
班级
ActionServlet
acts like FrontController pattern.
ActionServlet
就像FrontController 模式一样。
Image from this OnJava article.
来自这篇 OnJava 文章的图片。
回答by Paul
The struts Action class is effectively the Controller as it determines what should happen next in the processing of the request (from the browser). The Action class has an execute method that contains the controller logic. The Action class is a good example of the use of Command Pattern.
struts Action 类实际上是控制器,因为它决定了在处理请求(来自浏览器)中接下来应该发生什么。Action 类有一个包含控制器逻辑的 execute 方法。Action 类是使用Command Pattern 的一个很好的例子。
The struts-config.xml contains the routing information that determines which Controller (Action class) the request is forwarded to. It is good practice to have more than one controller, as a rule of thumb you have one controller per view, but this is not a strict rule and you may have more than one controller per view if the view is complex and has distinct features that merit separation of concerns within the controllers.
struts-config.xml 包含路由信息,用于确定将请求转发到哪个控制器(Action 类)。拥有多个控制器是一种很好的做法,根据经验,每个视图都有一个控制器,但这不是一个严格的规则,如果视图很复杂并且具有不同的特性,则每个视图可能有多个控制器控制器内的关注点分离。
Also, be careful not to burden your Controller classes with business logic, this leads to duplication of code within controllers when the code should be factored out to the business classes in the Model layer of your MVC application.
此外,请注意不要用业务逻辑给您的控制器类增加负担,这会导致控制器内的代码重复,当代码应该被分解到您的 MVC 应用程序模型层中的业务类时。
回答by gprathour
As you will be knowing MVC
stands for Model - View - Controller.
如您所知,MVC
代表模型 - 视图 - 控制器。
Simply saying, Model contains our business components and logic, View contains our Presentation technology and Controller controls the flow of control and working of the application.
简单地说,Model 包含我们的业务组件和逻辑,View 包含我们的 Presentation 技术,Controller 控制应用程序的控制和工作流程。
In Struts
在 Struts
There are two versions of Struts : Struts 1
and Struts 2
.
Struts 有两个版本:Struts 1
和Struts 2
.
**These two are different frameworks.
**这两个是不同的框架。
Struts 1 is based on Servlets. It has one ActionServlet
that acts as its controller.
Struts 1 基于 Servlet。它有一个ActionServlet
充当其控制器的角色。
Whereas in Struts 2 we have Filters. In this we can have Filter like FilterDispatcher
or StrutsPrepareAndExecuteFilter
that acts as our Controller.
而在 Struts 2 中,我们有过滤器。在这里,我们可以使用类似过滤器FilterDispatcher
或StrutsPrepareAndExecuteFilter
充当我们的控制器的过滤器。
**In Struts 2, Actions act as Model.
**在 Struts 2 中,动作充当模型。
The main job of Controller is to decide which Action class will handle which request And controller does this with the help of Configuration defined by us in struts.xml file or by annotations in case of Struts 2.
Controller 的主要工作是决定哪个 Action 类将处理哪个请求,并且控制器借助我们在 struts.xml 文件中定义的 Configuration 或在 Struts 2 的情况下通过注释来完成此操作。
回答by Clarence
A Struts-based Controller is a "component"
consisting of many parts. Custom Action classes
are written by application developers. Struts ActionServlet
is provided by the framework. A struts-config.xml
configuration file is written by application developers. The code that reads this file and creates Action**objects is provided by the framework. All of these elements together are the "Controller"
基于 Struts 的控制器"component"
由许多部分组成。Custom Action classes
由应用程序开发人员编写。Struts ActionServlet
由框架提供。一个struts-config.xml
配置文件是由应用程序开发人员编写的。读取此文件并创建 Action* *对象的代码由框架提供。所有这些元素加在一起就是"Controller"
For more information about the Struts Action package, see API docs below:
有关 Struts Action 包的更多信息,请参阅下面的 API 文档:
http://struts.apache.org/1.x/apidocs/org/apache/st...ts/action/package-summary.html
http://struts.apache.org/1.x/apidocs/org/apache/st...ts/action/package-summary.html
回答by Tigran Gularyan
struts.xml is the controller. You can have a look at this Struts 2 Architecture
struts.xml 是控制器。你可以看看这个Struts 2 Architecture
回答by Suryaprakash Pisay
As we know if we are using MVC there must be a front end controller we should declare in web.xml . i.e in struts2 there are two filters available
正如我们所知,如果我们使用 MVC,则必须在 web.xml 中声明一个前端控制器。即在 struts2 中有两个过滤器可用
Front End controllers
前端控制器
1)org.apache.struts2.dispatcher.FilterDispatcher 2)org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
1)org.apache.struts2.dispatcher.FilterDispatcher 2)org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
And these front end controller is responsible for loading struts.xml file
而这些前端控制器负责加载struts.xml文件
In struts.xml each formnameattributesare getting handled . After the operation is completed then controller will return to which page to be forwarded(Again this operation is taken care by front end controller).
在 struts.xml 中,每个表单名称属性都得到处理。操作完成后,控制器将返回要转发的页面(同样,此操作由前端控制器负责)。
Hope it helps
希望能帮助到你
回答by user2652879
The predefined servlet class is the controller which uses structs-config.xml to manage or control your whole application.
No, you can't have more than one controller in your struts application its just against the MVC rule (that you cant have more than one servlet in your app) struts is designed based on MVC.
预定义的 servlet 类是控制器,它使用 structs-config.xml 来管理或控制整个应用程序。
不,您的 struts 应用程序中不能有多个控制器,这违反了 MVC 规则(您的应用程序中不能有多个 servlet)struts 是基于 MVC 设计的。
回答by harry
The controller is responsible for intercepting and translating user input into actions to be performed by the model. The controller is responsible for selecting the next view based on user input and the outcome of model operations. The Controller receives the request from the browser, invoke a business operation and coordinating the view to return to the client.
控制器负责拦截用户输入并将其转换为模型要执行的操作。控制器负责根据用户输入和模型操作的结果选择下一个视图。Controller接收浏览器的请求,调用业务操作并协调视图返回给客户端。
whenever the request for any resource comes, first it goes to the controller. In simple words we can say that controller is a navigator. And its job is to find the corresponding action class for the incoming request and transfer it to that particular action. Or in technical word we can say that it performs the mapping.
每当对任何资源的请求到来时,它首先会到达控制器。简单来说,我们可以说控制器是一个导航器。它的工作是为传入的请求找到相应的动作类并将其转移到该特定动作。或者用技术术语来说,我们可以说它执行映射。
he controller is implemented by a java servlet, this servlet is centralized point of control for the web application. In struts framework the controller responsibilities are implemented by several different components like
控制器由一个 java servlet 实现,这个 servlet 是 web 应用程序的集中控制点。在 struts 框架中,控制器职责由几个不同的组件实现,例如
The ActionServlet Class The RequestProcessor Class The Action Class
ActionServlet 类 RequestProcessor 类 Action 类
for more reference on this you can go to the following link http://www.allapplabs.com/struts/struts_controller.htm
有关这方面的更多参考,您可以访问以下链接 http://www.allapplabs.com/struts/struts_controller.htm