Different Controllers in Spring
In Spring mvc, DispatcherServlet acts as controller and it will call the methods of controller classes that we have defined.
Controller classes:
these are the classes whose methods are called by DispatcherServlet . A controller class is the one which provides the implementation of org.springframework.web.servlet.mvc.Controllerinterface directly or indirectly.
In spring we have bunch of controller classes implementing org.springframework.web.servlet.mvc.Controller interface. You can go through the controller class hierarchy from the link.
The DispatcherServlet always calls handleRequest() of Controller interface when ever we send request from the browser to the controller.
Commonly used Controllers:
Some of the controllers frequently used are :
AbstractController,AbstractCommandController,AbstractFormController, MultiActionController,SimpleFormController,AbstractWizardFormController... etc.
All of these classes are implementing Controller interface. If we want to write our own controller we can extend any one of these classes , so that our class will become a controller class.
Illustration:
If our controller class is extending AbstractController then we need to implement handleRequestInternal() of AbstractController.
However, DispatcherServlet will call handleRequest() of Controller interface when ever we requested the controller. The handleRequest() internal code will call handleRequestInternal() of our AbstractController.
This can be represented as..
Now let us consider our class is extending AbstractCommandController, then we need to implement handle() of AbstractCommandController.
Similar to above illustration , DispatcherServlet always calls handleRequest() of Controller which will call handleRequestInternal() and which will further call handle() of our controller.
Suppose we have a form and we need to do some task after submitting the form then we will take the help of AbstractFormController.
showForm() method will be called to show the view(form) and if any errors exists in the validation they will be added to exception object.
Now whenever the form got submitted, DispatcherServlet will call handleRequest() which calls handleRequestInternal() which will further call processFormSubmission() of our controller.
If our form has to do multiple tasks , let's say form has more than one button, then we prefer MultiActionController.
Here add() and delete() are the custom methods corresponding to multiple buttons (add,delete) in our form.
If we want to display a form to the user and display a success message after reading the data from the form then we need to take the help of SimpleFormController.
We need to provide some basic information like CommandClass, Command name, Form view and Success view. Ofcourse we can set them in xml file also.
Whenever user submit's the form if no errors exists the controller calls onSubmit() of our controller class and finally it will display the success view to the user.
Now, if we want to get the information from user in multiple forms, then we need to go for AbstractWizardFormController. Incase of multiple form based applications our class should extend AbstractWizardFormController .
Here, we need to provide command class, command name and the pages are the forms to be displayed one by one in order to get multiple form application.
Which Controller is Best:
Based on our need, we can chose any of the controllers mentioned above. If we are performing a task without any form then we can use any of AbstractController,AbstractCommandController.
If our application have forms, then we can use any of SimpleFormController,AbstractFormController, AbstractWizardFormController based on our requirement.
Here we are using the term command class, command name.
A command class is a noraml java bean with some properties relative to the corresponding form. If the form has three fields then our command class need to have three corresponding properties. command name is the name given to the command class object.
No comments:
Post a Comment