How Java Spring MVC Works: Spring MVC Request Flow Explained Step by Step

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:

How Java Spring MVC Works

 

 

 

Explanation of Spring MVC Request Flow

  1. Client requests for a page by specifying the Web URL for the page. E.g. https://tutorialspedia.com
  2. 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).
  3. 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.
  4. Once Dispatcher Servlet has identified the Controller to be considered, it passes the client request to the controller.
  5. 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.

  1. 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.
  2. 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.
  3. This view is returned to the client and client can see the view and associated data on his browser.

Ajmal Abbasi

Ajmal Hussain Abbasi is Integration Consultant By Profession with 13+ years experience in Integration domain mainly with TIBCO products. He has extensive practical knowledge of TIBCO Business Works, TIBCO Cloud, TIBCO Flogo, TIBCO Mashery, TIBCO Spotfire, EMS and TIBCO ActiveSpaces. He has worked on a number of highly critical integration projects in various sectors by using his skills in TIBCO Flogo, TIBCO API Management (Mashery), TCI, Tibco Designer, TIBCO Business Studio, Adapters, TIBCO EMS, RV, Administrator, TIBCO BE, TIBCO ActiveSpaces etc. Ajmal Abbasi has experience with MuleSoft ESB as well. Ajmal Abbasi is also experienced in the area of API Management particularly with WSO2 API management platforms. Ajmal Abbasi is also experienced in developing solutions using Core Java and J2EE Technologies. You can contact Ajmal Abbasi for Consultancy, Technical Assistance and Technical Discussions.

More Posts - Website - Facebook - LinkedIn - YouTube

4 thoughts on “How Java Spring MVC Works: Spring MVC Request Flow Explained Step by Step

  1. Pingback: Spring Framework – Gowtham

Leave a Reply

Your email address will not be published. Required fields are marked *