Workflow of Spring Web MVC 

Spring Web MVC is a web framework that is built using a central front controller in the form of a Java servlet known as DispatcherServlet. This servlet is responsible for the orchestration of the underlying components required in order to process a request it receives from clients (in most cases from a browser). 

Spring Web MVC makes use of the following programming components of its own to make the web framework flexible and able to support different workflows:

  • HandlerMapping: This is used to map a request to a handler with a set of configurable pre-request and post-request interceptors. For example, a controller class with a @RequestMapping annotation will be mapped to a corresponding HTTP request by the RequestMappingHandlerMapping implementation of HandlerMapping.
  • Controller: This is used to implement handlers to a particular HTTP request, which will be responsible for coordinating the business logic and response generation. For example, an HTTP GET request to the URL /index can be mapped to the @GetMapping("/index") handler method in a controller class.
  • Model: This is used to send dynamic data in the form of attributes to View. Flash messages, lists of domain-specific objects, and so on can be sent using a Model.
  • ViewResolver: This is used to resolve view names usually returned by a controller handler method and get the actual View in order to return an HTTP response.
  • View: This is used to generate the final presentation to the end user. These views will contain the syntax that will use Model attributes to render responses dynamically. View technologies supported by Spring Web MVC include plain Java Server Pages (JSP) with JavaServer Pages Standard Tag Library (JSTL), Spring Thymeleaf, Apache Freemarker, and so on.

As an example, an HTTP GET request to path /index from a web browser will result in the following orchestration when the request reaches DispatcherServlet:

Let's understand the preceding diagram:

  • The browser will initiate the HTTP GET request for the path /index using the HTTP protocol
  • The HTTP GET request in the form of the HTTP protocol will be resolved and handed over to DispatcherServlet by the Web container (this is a required but transparent step)
  • DispatcherServlet will use the available HandlerMapping implementations to find a Controller that contains a handler method matching the path /index and HTTP request method GET
  • DispatcherServlet will invoke the handler method from the Controller and return the View name and Model data if there are any
  • DispatcherServlet will use any configured View Resolver to find the view by name and retrieve it
  • DispatcherServlet will use a model, if any are available, along with the resolved View to prepare the final view that will be rendered
  • DispatcherServlet will hand over the rendered View to the web container, which will convert it to an HTTP response and send it to the browser

This complex orchestration will take place for each HTTP request in a Spring Web MVC Framework-based application. 

..................Content has been hidden....................

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