Controller’s in the Spring MVC are mainly used for processing the requests and sending a model back to the views.
What happens when a Spring MVC controller extends another Controller.
When a Spring MVC controller extends another Controller, the functionality of the base controller can be directly used by the child controller using the request URL of the child controller.
Example
You have a Controller that does some action
1 2 3 4 5 6 7 8 9 |
@Controller @RequestMapping("/parent") public class ParentController { @RequestMapping("/someAction") public ModelAndView someAction() { // Some Code } } |
For calling the someAction() method you use the following URL
http://localhost:8080/<Project>/parent/someAction
Now you have some other Controller that also need to have the same functionality as the someAction() method.
Then those controllers can extend the ParentController and then use the someAction method as follows
1 2 3 4 5 6 7 8 9 10 |
@Controller @RequestMapping("/child") public class ChildController extends ParentController { @RequestMapping("/details") public ModelAndView getDetails() { // Some Code } } |
Since the ChildController extends the ParentController we can call the someAction() method of ParentController using the following URL. Note instead of parent in the URL we directly use the child request mapping url.
http://localhost:8080/<Project>/child/someAction
Controller extending can be useful when you have many pages that have same functionality across the pages and you need to reuse your code. This can be also achieved by adding the code in a Utility or Common class. you can use either of the 2.
Please do let me know if you need any more info in this or any articles available on this site.