<mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案。<mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。
并提供了:数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)。
后面,我们处理响应ajax请求时,就使用到了对json的支持。
后面,对action写JUnit单元测试时,要从spring IOC容器中取DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,来完成测试,取的时候要知道是<mvc:annotation-driven />这一句注册的这两个bean。

<context:annotation-config> declares support for general annotations such as @Required@Autowired@PostConstruct, and so on.

<mvc:annotation-driven /> is actually rather pointless. It declares explicit support for annotation-driven MVC controllers (i.e.@RequestMapping@Controller, etc), even though support for those is the default behaviour.

My advice is to always declare <context:annotation-config>, but don't bother with <mvc:annotation-driven /> unless you want JSON support via Jackson.

当我们需要controller返回一个map的json对象时,可以设定<mvc:annotation-driven />,

同时设定<mvc:message-converters> 标签,设定字符集和json处理类

目前理解如下:

<mvc:annotation-driven/>相当于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个bean,配置一些messageconverter。即解决了@Controller注解的使用前提配置。

<context:annotation-config/>是对包进行扫描,实现注释驱动Bean定义,同时将bean自动注入容器中使用。即解决了@Controller标识的类的bean的注入和使用。

一开始我在写配置的时候,只写了<context:component-scan/>,并没有使用<mvc:annotation-driven/>,servlet拦截*.do,.do请求可以被正确捕捉和处理。代码如下
mvc-servlet.xml

  1. <context:component-scan base-package="com"></context:component-scan>

web.xml

  1. <servlet>
  2. <servlet-name>mvc</servlet-name>
  3. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  4. <load-on-startup>1</load-on-startup>
  5. </servlet>
  6. <servlet-mapping>
  7. <servlet-name>mvc</servlet-name>
  8. <url-pattern>*.do</url-pattern>
  9. </servlet-mapping>

后来为了解决静态资源访问的问题,servlet改成了拦截所有请求,即/,并添加了默认的servlet,这时候*.do请求不能被控制器捕捉了,页面错误为404。直到添加了<mvc:annotation-driven/>之后,.do请求才又能被正确捕捉和处理。代码如下
mvc-servlet.xml

  1. <context:component-scan base-package="com"></context:component-scan>
  2. <mvc:annotation-driven/>
  3. <mvc:resources mapping="/styles/**" location="/WEB-INF/resource/styles/"/>
  4. <mvc:default-servlet-handler/>

web.xml

  1. <servlet>
  2. <servlet-name>mvc</servlet-name>
  3. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  4. <load-on-startup>1</load-on-startup>
  5. </servlet>
  6. <servlet-mapping>
  7. <servlet-name>mvc</servlet-name>
  8. <url-pattern>/</url-pattern>
  9. </servlet-mapping>

是什么原因造成这种区别的呢?为什么一开始没用<mvc:annotation-driven/>的时候可以,添加了默认servlet之后就不行了呢?

回答

最后的配置如果没有<mvc:annotation-driven/>,那么所有的Controller可能就没有解析,所有当有请求时候都没有匹配的处理请求类,就都去<mvc:default-servlet-handler/>即default servlet处理了。添加上<mvc:annotation-driven/>后,相应的do请求被Controller处理,而静态资源因为没有相应的Controller就会被default servlet处理。总之没有相应的Controller就会被default servlet处理就ok了。

------------------------------------------------

This tag registers the DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter beans that are required for spring MVC to dispatch requests to Controllers. 
这个标签注册了Spring MVC分发请求到控制器所必须的DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter实例

The tag configures those two beans with sensible defaults based on what is present in your classpath. 
标签配置的这2个实例可以根据classpath中的内容默认提供以下功能:

The defaults are:
1. Support for Spring 3's Type ConversionService in addition to JavaBeans PropertyEditors during Data Binding. 
A ConversionService instance produced by the org.springframework.format.support.FormattingConversionServiceFactoryBean is used by default. 
This can be overriden by setting the conversion-service attribute.
支持spring3的javaBeans属性编辑器数据绑定时的类型转换服务。
类型转换服务实例默认为org.springframework.format.support.FormattingConversionServiceFactoryBean。
可以覆盖conversion-service属性来指定类型转换服务实例类。

2. Support for formatting Number fields using the @NumberFormat annotation
支持@NumberFormat 注解格式化数字类型字段。

3. Support for formatting Date, Calendar, Long, and Joda Time fields using the @DateTimeFormat annotation, if Joda Time 1.3 or higher is present on the classpath.
@DateTimeFormat注解格式化 Date, Calendar, Long和 Joda Time(如classpath下存在Joda Time 1.3或更高版本)字段

4. Support for validating @Controller inputs with @Valid, if a JSR-303 Provider is present on the classpath. 
The validation system can be explicitly configured by setting the validator attribute.
支持@Valid注解验证控制器数据,classpath中需JSR-303的**。
可以使用setting明确的配置

5. Support for reading and writing XML, if JAXB is present on the classpath.
支持读写xml,classpath中需JAXB 。

6. Support for reading and writing JSON, if Jackson is present on the classpath.
支持读写json,classpath中需Jackson 。

A typical usage is shown below:
下边是用法:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- JSR-303 support will be detected on classpath and enabled automatically -->
<mvc:annotation-driven/>
</beans>

求上述1-6的使用例子。

总结:

要使用spring mvc中的@Controller注解,就必须要配置<mvc:annotation-driven />,否则org.springframework.web.servlet.DispatcherServlet无法找到控制器并把请求分发到控制器。

转自:http://www.iteye.com/problems/66133

  http://kingliu.iteye.com/blog/1972973

  http://www.noday.net/articles/2011/08/27/1314458126911.html

最新文章

  1. 文件权限及chmod使用方法
  2. PHP命名规范
  3. CPU的栈机制的一个小问题
  4. 关于oracle-12514错误的修改方法
  5. php基础22:上传并且保存文件
  6. UIButton详解
  7. html5的程序接口与元素变化
  8. .NET动态加载用户控件并传值的方法
  9. EntityFramework常用查询
  10. 一天一个Java基础——排序
  11. ACM2096_小明A+B
  12. Struts2之自定义局部类型转换器、全局类型转换器
  13. 播放视频的框架Vitamio的使用问题
  14. [Bayesian] “我是bayesian我怕谁”系列 - Exact Inferences
  15. JMeter基础教程2:正则表达式使用
  16. windows10禁用更新方法
  17. 5.03-requests_ssl
  18. CAS (多线程保证原子性)
  19. NABCD框架(作业和事件的定期提醒)及第八周学习进度条
  20. js--单例设计模式

热门文章

  1. Hadoop2 自己动手编译Hadoop的eclipse插件
  2. ASP.NET MVC Model绑定(二)
  3. Speedment -- 利用lambda编写SQL
  4. 微信JSAPI支付
  5. 在sql server中建存储过程,如果需要参数是一个可变集合怎么处理?
  6. dom addeventlistener与id 绑定事件的区别
  7. socket编程为什么需要htons(), ntohl(), ntohs(),htons() 函数
  8. MVC5 网站开发之七 用户功能 2 用户添加和浏览
  9. Linux内核配置、编译及Makefile简述
  10. 没有神话,聊聊decimal的“障眼法”