In this Spring MVC Architecture tutorial, we’ll delve into the foundational concepts of the framework, covering the Front Controller and MVC design patterns, as well as essential components like DispatcherServlet, HandlerMapping, HandlerAdapter, and ViewResolver. Furthermore, we’ll comprehensively explore the workflow of Spring MVC, from request reception to response delivery, accompanied by an illustrative diagram.
What is Spring MVC?
Spring MVC (or Spring Web MVC) is a module developed by the Spring team for web application development. This module incorporates two well-known enterprise design patterns:
Front Controller Pattern
The concept behind this pattern is to offer a unified entry point to an application. In other words, the front controller is tasked with mapping a request to the relevant resource or delegating this task to a specific component.
What is the idea behind the front controller pattern?
Let’s consider a scenario where we have multiple controllers handling client requests. In this setup, if we need to perform a security check for every request, we’d find ourselves repeating the same block of code in each controller. To mitigate this redundancy, the front controller pattern comes into play. This architectural approach channels all requests through a single controller. Consequently, if there’s a behavior that needs implementation—such as security measures or internationalization (i18n)—we only need to implement it in one centralized location.
MVC pattern
MVC stands for Model-View-Controller. This pattern was initially introduced in 1979 in the Smalltalk programming language by Trygve Reenskaug.
This pattern aims to organize application components into small, manageable units. Let’s delve into these components:
- View: This is the user interface presented to the user.
- Model: This component serves as the data holder, containing information to be displayed in the view.
- Controller: Responsible for gathering data, typically through the service or data access layers, and populating the Model Object. It then passes this data to the View for display.
How Spring combines those patterns
As mentioned, although the module is named Spring MVC, the framework primarily operates under the front controller pattern. In this pattern, a single component, the DispatcherServlet, manages all incoming requests. This component then delegates the requests to the appropriate handlers using a component known as the handler mapping. These handlers typically represent the controller’s methods (controller is special classes annotated with @Controller). the controller is responsible for populating the model with data and determining the suitable view to present to the user. making the application operate also under the MVC design pattern.
Let’s visualize the workflow to understand how Spring MVC works behind the scenes.
- The Front Controller intercepts all requests. In the case of Spring, it uses a class called DispatcherServlet as the Front Controller.
- DispatcherServlet consults the handler mappings registered in the ApplicationContext to determine which handler can handle the incoming request.
- After determining the appropriate handler mapping, DispatcherServlet calls the
getHandler()
method to retrieve a HandlerExecutionChain, which contains the handler (e.g., a controller method). - DispatcherServlet then determines the appropriate HandlerAdapter for the handler. For example, if the handler is a controller method, the RequestMappingHandlerAdapter will be used.
- DispatcherServlet calls the
handle()
method on the HandlerAdapter to execute the handler. - The controller method (handler) utilizes other objects, such as the service layer and data access layer, to gather the necessary data and add it to the Model. Subsequently, the controller returns the view name.
- The HandlerAdapter constructs a ModelAndView object using the Model and the view name returned by the controller method and returns it to DispatcherServlet.
- DispatcherServlet takes the ModelAndView object and consults the ViewResolver object to obtain the corresponding view.
- DispatcherServlet passes the Model contained in the ModelAndView object to the view and triggers the rendering process.
- The view renders the Model data and generates the output.
- DispatcherServlet returns the rendered view to the client.
Key Components of Spring MVC Architecture
Spring MVC Architecture is designed around the DispatcherServlet, which manages the entire flow of the application. This servlet works closely with several key components to ensure smooth request handling:
The Role of DispatcherServlet in Spring MVC Architecture
This is the primary component of the module. The DispatcherServlet class intercepts requests from clients and determines the appropriate handler (typically a controller method) using the HandlerMapping component. The controller object may delegate responsibilities to service objects.
The Role of HandlerMapping in Spring MVC Architecture
This component is responsible for identifying the resource responsible for handling a specific request, often based on the URL or other criteria. (When using @RequestMapping
or its shortcuts such as @GetMapping
, @PostMapping
, and so on, the RequestMappingHandlerMapping
implementation is utilized.)
HandlerExecutionChain
Each HandlerMapping has a HandlerExecutionChain that the DispatcherServlet uses to invoke the handler method. Additionally, it contains a list of interceptors that can be used for pre- and post-processing of the request.
The Role of HandlerAdapter in Spring MVC Architecture
The HandlerAdapter in Spring MVC serves as a pivotal component responsible for bridging the gap between the DispatcherServlet and various types of handlers, including controller methods and legacy controllers implementing the Controller interface. It determines the appropriate strategy for invoking the handler based on its type. Once the handler method is executed, the HandlerAdapter constructs and returns a ModelAndView object to the DispatcherServlet, encapsulating the processed model data and the view name. (When using @RequestMapping
or its shortcuts such as @GetMapping
, @PostMapping
, and so on, the RequestMappingHandlerAdapter
implementation is utilized.)
ModelAndView
This object encapsulates both the model data and the view name returned by the controller method. It serves as a container for carrying data from the controller to the view. The model typically contains attributes that need to be rendered in the view, while the view name represents the logical name of the view that should be rendered to the client.
NOTE: The ModelAndView
the object will be null. if we are using @RestController or@ResponseBody
because those annotations will force the handler method to write to the response body directly.
The Role of HandlerAdapter in Spring MVC Architecture
Once the controller has processed the request and constructed the ModelAndView object, the ViewResolver component comes into play. It resolves the logical view name provided by the controller into an actual View implementation, which is responsible for rendering the model data. The resolved view is then returned to the DispatcherServlet for rendering to the client, which could be in various formats such as HTML, JSP, JSON, or XML, depending on the type of view resolver and the configuration of the application.
Conclusion
Through this article, we’ve delved into the fundamental concepts and workflow of Spring MVC, exploring how it utilizes the DispatcherServlet to handle requests and orchestrates various components to achieve this. In our next Blog, we’ll transition to the practical realm, exploring the multitude of methods available for configuring the Spring MVC DispatcherServlet.