HandlerMapping和HandlerAdapter配置须知

---------------------siwuxie095

   

   

   

   

   

   

   

   

HandlerMapping 和 HandlerAdapter 配置须知

   

   

SpringMVC 的核心配置文件 dispatcher-servlet.xml 中,

HandlerMappingHandlerAdapter 的配置一共有 5 种方

式,具体如下:

   

方式一:什么都不配置

   

SpringMVC 针对这两者均已有默认配置,详见 spring-webmvc 的 jar

包中第一个包 org.springframework.web.servlet 中最后一个配置文件

DispatcherServlet.properties

   

   

   

   

   

方式二:仅限 XML 方式实现的 SpringMVC,配置如下:

   

BeanNameUrlHandlerMapping SimpleControllerHandlerAdapter

   

<!-- 配置 HandlerMapping(可选,即 可以省略不配置) -->

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

 

<!-- 配置 HandlerAdapter(可选,即 可以省略不配置) -->

<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>

   

   

   

方式三:仅限注解方式实现的 SpringMVC,配置如下:

   

DefaultAnnotationHandlerMapping AnnotationMethodHandlerAdapter

   

<!-- 配置 HandlerMapping(可选,即 可以省略不配置) -->

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

 

<!-- 配置 HandlerAdapter(可选,即 可以省略不配置) -->

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

   

「这两个类都已过期(废弃),所以不推荐此法」

   

   

   

方式四:仅限注解方式实现的 SpringMVC,配置如下:

   

RequestMappingHandlerMapping RequestMappingHandlerAdapter

   

<!-- 配置 HandlerMapping(可选,即 可以省略不配置) -->

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

 

<!-- 配置 HandlerAdapter(可选,即 可以省略不配置) -->

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>

   

   

   

方式五:使用 MVC 的注解驱动(此法通用),配置如下:

   

<!-- 启用注解驱动 -->

<mvc:annotation-driven/>

   

原理:详见 spring-webmvc 的 jar 包中第二个包 org.springframework.web.

servlet.config 中第一个类 AnnotationDrivenBeanDefinitionParser

   

   

   

   

   

   

   

   

   

   

【made by siwuxie095】

原文地址:https://www.cnblogs.com/siwuxie095/p/8486365.html