4.Spring MVC

Core components of Spring MVC

Nine core components:

  1. MultipartResolver: attachment resolver
  2. LocaleResolver :
  3. ThemeResolver
  4. HandlerMapping
  5. HadnlerAdpater
  6. HandlerExceptionResolver
  7. RequestToViewNameTranslator
  8. ViewResolver
  9. FlashMapManager

The most critical four HandlerMapping+HandlerAdapter+HandlerExceptinResolver

Describe the workflow of DispatcherServlet

  1. Client initiates a request: the request is captured by Spring MVC's DispatcherServlet
  2. Mapping processor: DispatcherServlet calls HandleMapping to obtain all related objects configured by the Handler according to the requested URL, and finally returns it in the form of a HandlerExcutionChain object (the HandlerExcutionChain object contains the corresponding Handler objects and interceptors)
  3. Processing adapter: DispatcherServlet selects an appropriate HandlerAdapter based on the Handler it has obtained (after successfully obtaining the HandlerAdapter, executes the interceptor's preHandler() method)
    and then fills in the Hadnler input parameters according to the model data in the request, and starts to execute the Handler(Controller) . At this time, in the process of filling the input parameters of the Handler, according to the configuration, Spring provides some additional work: such as data conversion, data formatting, and data verification. After the
    Handler (Controller) is executed, it will return a ModelAndView object to the DispatcherServlet
  4. Resolve the view: According to the returned ModelAndView, select a suitable ViewResolver (must be registered in the spring container), and after parsing the View object, return it to DispatcherServlet
  5. Render view + respond to request

Note: Due to the current separation of front and rear, spring will only be responsible for the Model and Controller, and the View will be handed over to the front end. Therefore, steps 4 and 5 may not be done. If there is an @ResponseBody annotation on the method, the parsed result will be directly written back to the client

WebApplicationContext

WebApplicationContext is a subclass that implements the ApplicationContext interface and is specially prepared for WEB applications:

  1. It allows to load configuration files from the path relative to the web root directory to complete the initialization of spring mvc self-check
  2. From the WebApplicationContext, you can get the ServletContext reference, and the entire web application context object will be placed in the ServletContext as an attribute, so that the application environment can access the spring context

Exception handling in Spring MVC?

Spring mvc provides the exception resolver HandlerExceptionResolver interface, which resolves the exception that occurs during the execution of the handler Hander into the corresponding ModelAndView

Spring MVC advantages

  1. Easy to use, you can use annotations to add some HTTP request method mapping methods and responses in different data formats
  2. Provide an interceptor mechanism, which can conveniently intercept requests
  3. Provide an exception mechanism to unify exception handling
  4. Different view technologies can be used

Is the Controller of Spring MVC a singleton?

In most cases, it is a singleton, but this needs to take into account the life cycle of Spring Bean

What is the difference between Spring MVC's interceptor and Filter?

  1. Same function
  2. The container is different: the interceptor is built in the spring mvc system, and the filter is built on the servlet container
  3. The ease of use is different: the interceptor provides three methods, which are executed at different times; the filter provides only one method

Guess you like

Origin blog.csdn.net/qq_37629227/article/details/112916912