tags:

views:

106

answers:

3

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.

+1  A: 

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.

jitter
+1  A: 
cetnar
A: 

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.

Paul