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

In Struts, application Controller layer code/functionality is split into two parts:

  • ActionServlet with RequestHandler
  • Action classes

ActionServlet acts like FrontController pattern.

alt text

Image from this OnJava article.

查看更多
小情绪 Triste *
3楼-- · 2020-03-15 07:20

The ActionServlet is the controller 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.

查看更多
在下西门庆
4楼-- · 2020-03-15 07:20

As you will be knowing MVC stands for Model - View - Controller.

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.

In Struts

There are two versions of Struts : Struts 1 and Struts 2.

**These two are different frameworks.

Struts 1 is based on Servlets. It has one ActionServlet that acts as its controller.

Whereas in Struts 2 we have Filters. In this we can have Filter like FilterDispatcher or StrutsPrepareAndExecuteFilter that acts as our Controller.

**In Struts 2, Actions act as Model.

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.

查看更多
SAY GOODBYE
5楼-- · 2020-03-15 07:20
  1. The predefined servlet class is the controller which uses structs-config.xml to manage or control your whole application.

  2. 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.

查看更多
神经病院院长
6楼-- · 2020-03-15 07:30

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"

For more information about the Struts Action package, see API docs below:

http://struts.apache.org/1.x/apidocs/org/apache/st...ts/action/package-summary.html

查看更多
爷的心禁止访问
7楼-- · 2020-03-15 07:32

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

Front End controllers

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

In struts.xml each form name attributes are 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).

Hope it helps

查看更多
登录 后发表回答