`

(转)使用@Controller注解为什么要配置<mvc:annotation-driven />

 
阅读更多

自己看了官方文档,也到网上查了下,目前理解如下:

<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

Java代码  收藏代码
  1. <context:component-scan base-package="com"></context:component-scan>  



web.xml

Java代码  收藏代码
  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

Java代码  收藏代码
  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

Java代码  收藏代码
  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://www.noday.net/articles/2011/08/27/1314458126911.html

分享到:
评论

相关推荐

    拦截器与冲突解决

    NULL 博文链接:https://xj84.iteye.com/blog/2065681

    (代码)SpringMVC第12讲:<mvc:annotation-driven/>

    SpringMVC第12讲:&lt;mvc:annotation-driven/&gt;

    SpringMVC源码总结(三)mvc:annotation-driven和mvc:message-converters简单介绍

    NULL 博文链接:https://lgbolgger.iteye.com/blog/2105151

    SpringMVC源码总结(二)mvc:mvc:annotation-driven背后的那些事

    NULL 博文链接:https://yihuawuye1.iteye.com/blog/2105063

    license.txt

    &lt;mvc:annotation-driven&gt; &lt;/mvc:annotation-driven&gt; 中配置Json格式乱码。代码如下: &lt;!--json格式乱码处理--&gt; &lt;mvc:message-converters register-defaults="true"&gt; &lt;bean class="org.springframework....

    SpringMVC+Hibernate实例

    &lt;mvc:annotation-driven/&gt; &lt;!--视图解析--&gt; &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="viewClass" value="org.springframework....

    Spring3中配置DBCP,C3P0,Proxool,Bonecp数据源

    &lt;mvc:annotation-driven /&gt; &lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt; &lt;mvc:default-servlet-handler /&gt; &lt;aop:config proxy-target-class="true"/&gt; &lt;tx:annotation-driven ...

    SpringMVC+Hibernate全注解整合

    &lt;mvc:annotation-driven/&gt; &lt;!-- 扫描包 --&gt; &lt;context:annotation-config/&gt; &lt;context:component-scan base-package="com.org.*" /&gt; &lt;bean id="jspViewResolver" class="org.springframework.web.servlet.view...

    springboot 基础简易实例, maven项目

    @Controller public class SpringBootController { /** * 本地访问内容地址 :http://localhost:8080/hello2 ; 可访问到 * src/main/resources/views/index.html */ @RequestMapping("/hello2") public String ...

    spring 定时任务

    本例中一共使用了两种 spring 的定时任务,一种是使用 xml 配置的定时任务,一种是使用 annotation 配置的定时任务...&lt;task:annotation-driven/&gt; 在定时任务的方法上配置 @Scheduled(cron="0 */10 * * * *") 就可以了。

    基于MyEclipse搭建maven+springmvc整合图文教程(含源码0

    &lt;mvc:annotation-driven /&gt; &lt;context:component-scan base-package="Controller" /&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="prefix" value="/...

    Spring MVC 教程 快速入门 深入分析

    Spring MVC 教程 快速入门 深入分析 目录 一、前言 二、spring mvc 核心类与接口 三、spring mvc 核心流程图 四、spring mvc DispatcherServlet说明 ...十九、 &lt;mvc:annotation-driven /&gt; 到底做了什么工作

    springMVC技术概述

    springMVC相关技术配置使用注解的HandlerMapping和HandlerAdapter使用&lt;mvc:annotation-driver&gt; 不过springBoot已经省略了这些配置 配置使用注解的Handler和Service等等使用&lt;context:component-scan&gt; 不过springBoot...

    Spring MVC入门教程

    个人认为相当适合入门和知识巩固!! 一、前言 二、spring mvc 核心类与接口 ...十九、 &lt;mvc:annotation-driven /&gt; 到底做了什么工作 二十、 本文中springMVC.xml配置文件是核心,这里给一个下载地址

    SpringMVC入门教程

    SpringMVC: 一、前言 二、spring mvc 核心类与接口 三、spring mvc 核心流程图 ...十九、 &lt;mvc:annotation-driven /&gt; 到底做了什么工作 二十、 本文中springMVC.xml配置文件是核心,这里给一个下载地址

    SpringMVC框架架构介绍

    一、前言 二、spring mvc 核心类与接口 三、spring mvc 核心流程图 四、spring mvc ...十九、 &lt;mvc:annotation-driven /&gt; 到底做了什么工作 二十、 本文中springMVC.xml配置文件是核心,这里给一个下载地址

    struts2.3+hibernate3.6+spring3.1整合的纯xml配置的小项目

    &lt;tx:annotation-driven transaction-manager="txManager" /&gt; &lt;!-- &lt;aop:config&gt; &lt;aop:pointcut id="defaultServiceOperation" expression="execution(* x.y.service.*Service.*(..))" /&gt; &lt;aop:pointcut id=...

    spring3.2+strut2+hibernate4

    &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt; &lt;!-- 保证POJO中标注@Required的属性被注入 --&gt; &lt;bean class="org.springframework.beans.factory.annotation....

    维生药业小项目 SSH简单学习项目

    &lt;tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /&gt; &lt;!-- 使用annotation 自动注册bean,并检查@Required,@Autowired的属性已被注入 --&gt; &lt;context:component-scan...

Global site tag (gtag.js) - Google Analytics