Controller in Struts

2020-03-15 07:05发布

What is Controller in MVC ?

Is it struts.xml or Servlet (Action Class)?

Can we have more than one Controller in our application?

Is it good practice to have more than one controller?

9条回答
贪生不怕死
2楼-- · 2020-03-15 07:36

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.

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

The ActionServlet Class The RequestProcessor Class The Action Class

for more reference on this you can go to the following link http://www.allapplabs.com/struts/struts_controller.htm

查看更多
冷血范
3楼-- · 2020-03-15 07:37

struts.xml is the controller. You can have a look at this Struts 2 Architecture

查看更多
够拽才男人
4楼-- · 2020-03-15 07:37

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.

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.

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.

查看更多
登录 后发表回答