Introduction and Workflow of Spring MVC

Spring provides Model-View-Controller (MVC) architecture, and components that can be used to develop flexible and loosely coupled web applications. It uses the features of Spring core features like IoC and DI.

  • The Model encapsulates the application data and in general they will consist of POJOs.

  • The View is responsible for rendering the model data and in general it generates HTML output that the client’s browser can interpret.

  • The Controller is responsible for processing user requests and building an appropriate model and passes it to the view for rendering.

The Spring Web MVC framework is designed around the DispatcherServlet.

Java Servlet are programs that act as a middle layer between a request coming from a Web browser or HTTP client, and applications on the HTTP server.

What happens behind the scene when an HTTP request is sent to the server

spring mvc workflow

  • The DispatcherServlet handles all the HTTP requests and responses.

  • After receiving an HTTP request, the DispatcherServlet consults the HandlerMapping to call the appropriate Controller.

  • The Controller takes the request, calls the appropriate method, and builds the model and returns the view page name to the DispatcherServlet.

  • The DispatcherServlet will then take help from the ViewResolver to pick up the defined view page for the request.

  • Once the view is finalized, the DispatcherServlet passes the model data to the view, which is finally rendered on the browser.

All the above-mentioned components (i.e. HandlerMapping, Controller, and ViewResolver) are parts of WebApplicationContext, which is an extension of the ApplicationContext with some extra features necessary for web applications.