Spring MVC architecture

In the Spring MVC framework, there are multiple core components that maintain the flow of request and response execution. These components are clearly separated and have different interfaces and implementation classes, so they can be used according to requirements. These core components are as follows:

Component

Summary

DispatcherServlet

It acts as a front controller of the Spring MVC framework through the life cycle of HTTP requests and responses.

HandlerMapping

When a request comes, this component is responsible for deciding which controller will handle the URL.

Controller

It executes the business logic and maps the resultant data in ModelAndView.

ModelAndView

It holds the model data object in terms of the execution result and the view object to render.

ViewResolver

It decides the view to be rendered.

View

It shows the result data from a model object.

 

The following diagram illustrates the flow of the preceding components in the Spring MVC architecture:

Spring MVC architecture

Let's understand the basic flow of the architecture:

  1. When the incoming request comes, it is intercepted by the front controller, DispatcherServlet. After intercepting the request, the front controller finds the appropriate HandlerMapping.
  2. The HandlerMapping maps the client request call to the appropriate Controller, based on the configuration file or from the annotation Controller list, and returns the Controller information to the front controller.
  3. The DispatcherServlet dispatches the request to the appropriate Controller.
  4. The Controller executes the business logic defined under the Controller method and returns the resultant data, in the form of ModelAndView, back to the front controller. 
  5. The front controller gets the view name based on the values in the ModelAndView and passes it to the ViewResolver to resolve the actual view, based on the configured view resolver.
  6. The view uses the Model object to render the screen. The output is generated in the form of HttpServletResponse and passed to the front controller.
  7. The front controller sends the response back to the servlet container to send the output back to the user.

Now, let's understand the Spring MVC configuration methods. Spring MVC configuration can be set up in the following ways:

  • XML-based configuration
  • Java-based configuration

Before we start with the configuration using the preceding methods, let's define the steps that are involved in setting up the Spring MVC application:

  1. Configuring front controller
  2. Creating Spring application context
  3. Configuring ViewResolver
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset