Spring MVC (Model View Controller) is a well-known and widely used framework for developing web applications with loosely coupled and well organised layering of presentation, persistence and controller layer. With a layered flexible architecture of Spring MVC, it becomes very convenient for developers to isolate the bricks and pieces and combine them together in the form of a professional web application. A web application developed following MVC standard is easier to scale, update and manage as a loosely coupled layered web application is much easier to modify to introduce any changes at a certain layer.
In this post, I will explain flow of requests in a typical Spring MVC application. By the end of this tutorial, you will be knowing the whole life cycle of a web request for Spring MVC based web applications and you will have a fair idea How Java Spring MVC Works and how Spring Web MVC framework distributes the functionalities to loosely couple the layered pieces.
Spring MVC request flow is depicted in the diagram below:
Explanation of Spring MVC Request Flow
- Client requests for a page by specifying the Web URL for the page. E.g. https://tutorialspedia.com
- Client request is intercepted by the Dispatcher Servlet also known as Front Controller. Dispatcher Servlet is a servlet specified in Web.XML file (for XML Based configurations) or in the Web Configuration class (for java based configurations).
- Dispatcher Servlet uses URL Mapping Handler to find out the relevant controller class to which request should be passed for subsequent processing. For example, If you have a Controller defined for all requests by specifying “/” in the URL, all requests will be entertained by that controller.
- Once Dispatcher Servlet has identified the Controller to be considered, it passes the client request to the controller.
- The controller class is the main class controlling the business logic flow once request has been dispatched it it by dispatcher servlet. This class will implement the methods for different type of http requests (e.g. GET, POST) and all logic to call Service layer methods will reside in this controller class.
The controller class will also be responsible for returning the ModelAndView object back to the dispatcher servlet after getting all business logic executed and any data returned from DAO layer. ModelAndView object returned by the controller back to the controller specified both view and model objects.
Controller class is annotated by @Controller annotation.
- After receiving ModelAndView object from the controller, Dispatcher Servlet now sends model object to view resolver to get the name of the view which needs to be rendered.
- Once the view to be rendered has been identified, Dispatcher Servlet passes model object to the view. Model object contains the data which needs to be displayed in the view. View will be rendered with the model data. Views can be designed in any front-end technology.
- This view is returned to the client and client can see the view and associated data on his browser.
Helped me a lot to understand!
Glad to hear that, Serin.
Pingback: Spring Framework – Gowtham
nice explanation