Detailed Explanation of Three Components of Spring Mvc

Keywords: Spring JSP Dubbo xml

Spring Mvc Framework Structure Diagram

  • Processor mapper: mapping user request path to Controller method
  • Processor adapters: Find different processor adapters according to different zones of handler(controlelr class) development (annotation development/other development)
  • View parser: According to the type of view address file returned by handler (jsp/pdf... To find the corresponding view parser for parsing

Key Points of Spring Mvc Framework Configuration 1: Processor mappers and processor adapters should be configured in the core configuration file of SpirngMvc, otherwise Spring Mvc will go to / org/spring framework/web/servlet/Dispatcher Servlet.properties file to find processor mappers and processor adapters in turn, so every time please The efficiency will be slow if all the cities are judged in turn.

<! -- Annotated processor mappers open source code and find it outdated - >
<!--         <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean> -->
<! - Annotated processor adapter open source found out to be obsolete - > ___________
<!--         <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean> -->
<! - Configure the latest version of Annotated Processor Mapper
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> -->
<! - Configure the latest version of the annotated processor adapter - >
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> 

However, the above configuration of the latest version of Annotation Processor Mapper and Processor Adapter is still not good. If the method will be upgraded after the official subsequent version upgrade, the class will still or may be outdated. It will be troublesome to modify it in the project at this time. At this time, we can configure only one Annotation Driver.

    <! - Annotation driven:
        Function: Automatically configure the processor mapper and adapter of the latest annotations for us
     -->
    <mvc:annotation-driven></mvc:annotation-driven>

Spring Mvc Configuration View Parser is available or not

    <! -- Configure the View Parser 
    Function: When specifying the page path in the controller, you don't need to write the full path name of the page, you can write the page directly to remove the name of the extension.
    -->
            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <! - Real page path = prefix + page name with suffix name removed + suffix - >
        <! - prefix - >
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <! - Suffix - >
        <property name="suffix" value=".jsp"></property>
    </bean>

Here is the complete core configuration file for Spring Mvc

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://code.alibabatech.com/schema/dubbo 
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">

        <!-- To configure@Controller Annotation Scanning -->
        <context:component-scan base-package="cn.itheima.controller"></context:component-scan>


    <!-- Annotation driven:
        //Function: Automatically configure the processor mapper and adapter of the latest annotations for us
     -->
    <mvc:annotation-driven></mvc:annotation-driven>


    <!-- Configuration view parser 
    //Function: When specifying the page path in the controller, you don't need to write the full path name of the page, you can write the page directly to remove the name of the extension.
    -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- Real Page Path =  prefix + Remove the suffix name from the page name + Suffix -->
        <!-- prefix -->
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <!-- Suffix -->
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

Posted by irvieto on Mon, 02 Sep 2019 03:59:22 -0700