SpringMVC第四篇——处理器映射器,处理器适配器,视图解析器的配置

这两个注解的作用和来源:

<mvc:annotation-driven/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value=""/>
            <property name="suffix" value=""/>
        </bean>

1:默认加载的映射器处理器和映射器配置器

入门程序只是配置了组件扫描器
使用`<context:component-scan>`自动扫描标记@Controller的控制器类,
并没有配置处理器映射器和处理器适配器;

根据SpringMVC的架构和执行流程,是前端控制器调用处理器映射器,调用处理器适配器,执行处理器。是因为SpringMVC中在加载完前端控制器之后,会默认的加处理器映射器和处理器适配器;

SpringMVC加载.DispatcherServlet.properties配置文件:

这里写图片描述

org.springframework.web.servlet.HandlerMapping=org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,\org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
映射器:默认加载两个处理器映射器。

处理器映射器

使用配置方式开发:(需要实现implements Controller)

org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,

使用注解方式开发:

org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
处理器适配器
org.springframework.web.servlet.HandlerAdapter=
org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,\
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,\
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

注解方式就回加载这个适配器:

org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

如果controller的实现使用的是:

public class Show  implements HttpRequestHandler{},

不使用@controller的注解,那么就会加载

org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,\ ,这个处理器适配器:

如果使用的是:

public class Show  implements Controller{}
就会加载:  org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,\

我们使用的是:@controller,所以加载的是:

 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

2:关联源码,查看加载的类,

打开注解方式的默认的映射器处理器源码:

org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping

关联源码包:
spring-webmvc-4.1.3.RELEASE-sources.jar

发现:
这里写图片描述

*默认的处理器映射器: `DefaultAnnotationHandlerMapping已经过期了,但是还是能够使用:
在spring3.2和之后的之后的版本,使用的是

**org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping**

同样打开适配器源码:默认的处理器适配器:AnnotationMethodHandlerAdapter
所以当我们使用controller的注解类,使用requestMapping的注解方法。那么项目运行起来,前端控制器加载之后,就会默认的加载了处理器映射器org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
和处理器适配器。org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter

2:如果我们要使用新的处理器映射器:
那我们就不使用默认的,我们自己进行配置:

 <!-- 处理器映射器 -->
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> 
        <!-- 处理器适配器 -->
  <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> 

这样就前端控制器加载之后,就会加载我们配置的处理器映射器和处理器适配器。

这样使用比较麻烦:使用注解驱动代替上面的配置

 <mvc:annotation-driven/>

自动加载RequestMappingHandlerMapping和RequestMappingHandlerAdapter这两个类

3:视图解析器的配置

  <!-- 视图解释器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/"/><!-- 前缀 -->
            <property name="suffix" value=".jsp"/><!-- 后缀 -->
        </bean>

就是会在返回的所有的视图路径上自动加上前缀和后缀
入门程序中:返回的视图路径:

ma.setViewName("/WEB-INF/hello.jsp");

配置好了视图解析器之后因为会自动加上前缀和后缀。只需要写成:

ma.setViewName("hello");

4.总结:

1:使用注解方式进行SpringMVC进行开发的时候,不配处理器映射器和处理器适配器,框架会默认加载DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter。
2:从spring3.1之后这两个类就过期了,但是还能使用。我们开发时候使用新的类。需要在配置文件中进行配置。我们使用注解驱动:

 <mvc:annotation-driven/>

这个配置是替代了配置:

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

3:视图解析器配置:

        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value=""/><!-- 前缀 -->
            <property name="suffix" value=""/><!-- 后缀 -->
        </bean>

视图解析器,可以不用配置,是为了方便开发使用,

返回 SpringMVC的学习笔记目录

http://blog.csdn.net/m15517986455/article/details/79319601

  • 6
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值