Introduction to Spring MVC
By: Dasari Swaroop Kumar
Tags: Introduction, Spring Mvc, Spring Framework

Model View Controller (usually known as MVC) is a software design pattern commonly used for developing user interfaces which divides the related program logic into three interconnected elements. This is done to separate internal representations of information from the ways information is presented to and accepted from the user.

Above is the definition of MVC provided in Wikipedia. MVC is a software design pattern intended to develop web-based applications. It segregates an application into three components — Model, View, and Controller. Each of these components is assigned with a defined responsibility. For instance, a model manages the application data and decouples the data from its representations (views). On the other hand, a view is where we present the model data. It can be a UI component (JSP/HTML page), an XML or PDF document, JSON data, or an RSS or Atom feed. Controller whereas accepts input and converts it to the command for model or view.

Spring WebMVC or commonly knows as Spring MVC is an implementation of this MVC design pattern build on top of Servlet API and integrated with the Spring framework.

What is the Front Controller Design pattern?

We are discussing it here as Spring MVC is based on this design pattern. Front Controller is a standard design pattern where a single controller (Servlet in our context) is responsible for handling all request processing. The actual processing is done by different other configurable delegated components.

Dispatcher Servlet

DispatcherSevlet (It’s a standard Java Servlet) is the front controller in Spring MVC. A dispatcher servlet can be configured by means of a web.xml, ServletContainerInitializer, or through Spring’s AbstractAnnotationConfigDispatcherServletInitializer

Front Controller

The adjacent diagram provides an overview of the role of the Dispatcher Servlet and its communication with the controller and view components. All requests to the application are attended by the Front Controller. The request is then delegated to a controller, which returns a model containing the data to be rendered. The model is then provided to the view template for rendering. Finally, the Front Controller sends out the response to the caller.

Communication between components in Spring MVC

In this section, we will present the interaction between various components in Spring MVC. However, before proceeding further, let us understand the actors:-

User: Application user/API

Front Controller: Spring DispatcherServlet responsible for accepting the request from users and delegate and orchestrates the request with other internal components for processing

Handler Mapper: Responsible to find the appropriate controller that can serve the incoming request

Controller: Processes the request. It invokes the underlying business and repository services. It warps the response data in a model

View Resolver: Responsible to find the appropriate view which can handle the request

View: The component on which the processed data (the model returned by the controller) is rendered

Spring MVC — Request processing flow

The above diagram explains the sequence of events internally managed in a Spring MVC application:-

An application user/API access a resource or URL in the Spring MVC application. This service request is attended by DispatcherServlet It asks the HandlerMapper to find a suitable handler that can address the request HandlerMapper returns the controller that can handle the request DispatcherServlet then forward the request to the controller to service the request The controller performs the necessary processing and packs the response data in a model and hand it over to DispatcherServlet DispatcherServlet then asks the ViewResolver to find the appropriate view which can handle the request ViewResolver resolves the appropriate view and sends it back to DispatcherServlet DispatcherServlet then hand over the model to view and asks it to render View returns the response back to DispatcherServlet DispatcherServlet returns the response back to the user/API

Conclusion

In this article, we introduced Spring MVC and presented the core components of Spring MVC. We then explained the role of various internal components and provided an overview of how a request is processed in a traditional Spring MVC application.