Day24 SpringMVC execution process details

SpringMVC execution process in detail

Insert picture description here

  • Name meaning (understanding)
    1) HandlerMapping: The processor mapper
    HandlerMapping is responsible for finding the Handler that is the processor according to the user's request url, ie 处理@RequestMapping 注解,并将其注册到请求映射表中. Springmvc provides different mappers to implement different mapping methods, such as: configuration file mode, implementation interface mode, annotation mode, etc.
    2) Handler:
    Handler is a back-end controller following the DispatcherServlet front-end controller. Handler processes specific user requests under the control of DispatcherServlet.
    3) HandlAdapter: The processor adapter executes the processor
    through the HandlerAdapter 处理请求的适配器,确定调用哪个类的哪个方法,并且构造方法参数,返回值. This is an application of the adapter mode, and more types of processors can be executed by extending the adapter.
    4) ViewResolver: The
    View Resolver is responsible for generating the View view from the processing result. The View Resolver first resolves the logical view name into the physical view name, that is, the specific page address, and then generates the View view object, and finally renders the View to pass the processing result through The page is displayed to the user.
    5) View: View
    springmvc framework provides many types of View support, including: jstlView, freemarkerView, pdfView, etc. Our most commonly used view is jsp.

  • (1) Three more objects than the simple diagram
    "HandlerMapping processor mapper: create address and method mapping
    " HandlerAdapter processor adapter: call methods based on address
    "ViewResolver view resolver: handle ModelAndView

  • (2) Implementation process

1. The user sends a request to the front controller DispatcherServlet

2. DispatcherServlet receives the request to call the HandlerMapping processor mapper.

3. According to the processor mapper 请求url找到具体的处理器, the processor object and the processor interceptor (if any) are generated and returned to the DispatcherServlet.

4. DispatcherServlet calls the processor through the HandlerAdapter processor adapter

5. Execution processor (Controller, also called back-end controller).

6. Controller execution is complete and returns to ModelAndView

7, HandlerAdapter returns the controller execution result ModelAndView to DispatcherServlet

8. DispatcherServlet passes ModelAndView to ViewReslover view parser

9, ViewReslover parses and returns to the specific View

10. DispatcherServlet renders the View (that is, fills the model data into the view).

11. DispatcherServlet responds to users

Since the three major components provided by default in SpringMVC may be out of date, we can manually set these three components

Insert picture description here

  • Configure the processor mapper and processor adapter
<!--方式1:手动一个个设置 -->
    <!--处理器映射器-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
    <!--处理器适配器-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

<!--方式2:手模型驱动配置处理器映射器和处理器适配器 -->
    <!--注意,这里在选择mvc标签时,一定要选择最后的路径是/mvc的选项-->
    <!-- 这行配置可以替换掉上边的两行配置-->
    <mvc:annotation-driven/>
  • Configure view resolver
<!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--定义页面路径的前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--定义页面路径的后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>
  • If you cannot use the mvc tag, import the following constraints
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<!--进行包扫描,创建controller层的对象 只要是指定的包与子包下面有@Controller的类
    都要被创建对象,且加入ioc容器中-->
    <context:component-scan base-package="cn.cyl.controller"/>
    
	<!--配置处理器映射器和处理器适配器 -->
	<!--开启SpringMVC注解的支持 @RequestMapping @RequestBody @ResponseBody-->
	<mvc:annotation-driven/>

	<!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

Because the view resolver is configured, the prefix and suffix of the page path are defined. When using the setViewName method for the Model object in the future, do not write the full path.
Such as: mv.setViewName("WEB-INF/jsp/list.jsp"); Change to mv.setViewName("list");

Guess you like

Origin blog.csdn.net/qq_43639081/article/details/109149092