使用SpringMVC框架时发现有的配置了HandlerMapping,而有的没有,那么它们有什么区别呢?不配置能不能正常使用框架呢?

下面我们看一看不配置任何HandlerMapping时,框架会使用什么?

下面我们注释掉框架中配置HandlerMapping的代码

<?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
4 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
5 xsi:schemaLocation="
6 http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans.xsd
8 http://www.springframework.org/schema/context
9 http://www.springframework.org/schema/context/spring-context.xsd
10 http://www.springframework.org/schema/mvc
11 http://www.springframework.org/schema/mvc/spring-mvc.xsd">
12
13 <context:component-scan base-package="com.springmvc.demo.controller" />
14
15 <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
16 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" /> -->
17 <!-- <mvc:default-servlet-handler/> -->
18 <!-- <mvc:annotation-driven /> -->
19 <!-- <mvc:resources location="/resource/images/" mapping="/images/**" /> -->
20 <bean id="/simpleController" class="com.springmvc.demo.controller.SimpleController"></bean>
21
22 <!-- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
23 </bean> -->
24
25 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
26 <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
27 <property name="prefix" value="/" />
28 <property name="suffix" value=".jsp" />
29 </bean>
30
31
32 </beans>

这次我们打一断点调试一下看看使用了什么HandlerMapping:

可以看到注册了两个HandlerMapping,所以:

1.访问/simpleController,正常

2.访问/user/preAddUser(通过@RequestMapping注解的)也正常,因为DefaultAnnotationHandlerMapping也可以处理注解,这个还是spring3.1之前处理注解的一个类,3.1及以后改为了RequestMappingHandlerMapping这个类,查看api可以看到这个类【已经过期】,默认情况继续使用这个类可能是为了向后兼容。

其实这是一个默认配置,位置在spring-webmvc.jar里面,如果你没有显式注册一个HandlerMapping,那么就会使用org.springframework.web.servlet.DispatcherServlet.properties这个里面默认的配置,配置如下:

org.springframework.web.servlet.HandlerMapping=org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,\
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping

所以就会注册上面两个RequestMapping。

最新文章

  1. 用Taurus.MVC 做个企业站(上)
  2. C#抓取天气数据
  3. NYOJ题目77开灯问题
  4. js中对类和对象的理解
  5. bzoj 2743 树状数组离线查询
  6. Spring MVC配置DispatcherServlet的url-pattern
  7. HDU 4941 Magical Forest 【离散化】【map】
  8. How to: Reading an ini config file from a batch file
  9. Java security MD5加密算法
  10. POJ 1724 最短路费用限制
  11. Java format 简单应用
  12. UIColor,CGColor,CIColor三者间的区别和联系
  13. UIImage 和 UIImageView区别
  14. Win10下 VS2017 安装失败 未能安装包“Microsoft.VisualStudio.AspNet45.Feature,version=15.0.26208.0”
  15. Linux常见命令(四)——mkdir
  16. Lucene初体验——Hello Word实现
  17. caffe服务器搭建血泪记录
  18. python 模块之-json
  19. Logstash配置文件介绍
  20. 030 分布式集群中,设定时间同步服务器,以及ntpd与ntpdate的区别

热门文章

  1. python(17)- 迭代器和生成器及应用
  2. C#自定义类型数组排序
  3. 【NoSql】Redis实践篇-简单demo实现(一)
  4. Deployment相对ReplicaSet优势
  5. 对canvas arc()中counterclockwise参数的一些误解
  6. GreenPlum 安装方法详解
  7. Java UUID 生成(转载)
  8. Android UI经常使用实例 怎样实现欢迎界面(Splash Screen)
  9. Git检出远程库的分支等相关操作
  10. [IR课程笔记]向量空间模型(Vector Space Model)