What is Controller in MVC ?
Is it struts.xml
or Servlet
(Action Class)?
Can we have more than one Controller
in our ap
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
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.
In Struts, application Controller layer code/functionality is split into two parts:
ActionServlet
with RequestHandler
Action
classesActionServlet
acts like FrontController pattern.
Image from this OnJava article.
struts.xml is the controller. You can have a look at this Struts 2 Architecture
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.
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.