Spring MVC and Request Life Cycle

Spring MVC and Request Life Cycle

Spring MVC and Request Life Cycle

1. Overview

1.1 Spring MVC

Spring MVC is a web application framework that implements the Model View Controller pattern. It allows to implement controller classes equipped with mapping annotations to map HTTP requests into method invocations and bind request parameters and payloads to method arguments.

@RestController // 1 class MyController { @GetMapping("/hello") // 2 String sayHelloTo(@RequestParam Optional name) { // 3 return String.format("Hello, %s!", name.orElse("world")); } }

@RestController: An annotation to make the component known to the framework and assign it a given role (here: a Spring WebMVC controller).

@GetMapping: An annotation to make the component known to the framework and assign it a given role (here: a Spring WebMVC controller).

@RequestParam: An annotated parameter to express we want to get access to the request parameter named name. Wrapped into an Optional as the request might not include that parameter and we have to handle that case in the implementation.

2. Components

2.1 Filter

A filter is an object that performs filtering tasks on either the request to a resource (a servlet or static content), or on the response from a resource, or both.

The filter applies to every request

Filter precede DispacherServlet

A filter is a J2EE standard specification

2.2 Dispatcher servlet

The servlet analyzes the requests and dispatches them to the appropriate controller for processing.

Request-driven, designed around a central servlet that dispatches, or delegate, requests to controllers and offers other functionality that facilitates the development of web applications

An actual Servlet (it inherits from the HttpServlet base class), and as such is declared in the web.xml of your web application

You need to map requests that you want the DispatcherServlet to handle, by using a URL mapping in the same web.xml file

example org.springframework.web.servlet.DispatcherServlet 1 example *.form

2.3 Common Service

The common services will apply to every request to provide supports including i18n, theme, file upload, and so on. Their configuration is defined in the DispatcherServlet's WebApplicationContext.

2.4 Handler mapping

This maps the request to the handler (a method within a Spring MVC controller class). Since Spring 2.5, in most situations, the configuration is not required because Spring MVC will automatically register the org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping class that maps handlers based on HTTP paths expressed through the @RequestMapping annotation at the type or method level within controller classes.

2.5 Handler Interceptor

In Spring MVC, you can register interceptors for the handlers for implementing common checking or logic such as checking and ensuring that only the handlers can be invoked during office hours.

2.6 Handler exception resolver

In Spring MVC, the HandlerExceptionResolver interface (under the package org.springframework.web.servlet) is designed to deal with unexpected exceptions thrown during request processing by handlers.

2.7 View resolver

Spring MVC's ViewResolver interface (under the package org.springframework.web.servlet) supports view resolution based on a logical name returned by the controller.

3. Request Life cycle Procedure

4. Reference

http://static.olivergierke.de/lectures/spring/#spring.psa

https://justforchangesake.wordpress.com/2014/05/07/spring-mvc-request-life-cycle/

https://javaee.github.io/javaee-spec/javadocs/javax/servlet/Filter.html

https://docs.spring.io/spring/docs/3.0.0.M4/spring-framework-reference/html/ch15s02.html

https://www.journaldev.com/1933/java-servlet-filter-example-tutorial

https://www.logicbig.com/tutorials/spring-framework/spring-web-mvc/spring-mvc-intro.html

from http://demyank.tistory.com/487 by ccl(A) rewrite - 2020-03-07 00:55:34

댓글

이 블로그의 인기 게시물

2020 LCK 롤챔스 Spring 경기 재개 및 일정

데이터 바인딩 추상화 - propertyEditor

Spring Web Form