- <context:component-scan base-package="com"></context:component-scan>
- <servlet>
- <servlet-name>mvc</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>mvc</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- <context:component-scan base-package="com"></context:component-scan>
- <mvc:annotation-driven/>
- <mvc:resources mapping="/styles/**" location="/WEB-INF/resource/styles/"/>
- <mvc:default-servlet-handler/>
- <servlet>
- <servlet-name>mvc</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>mvc</servlet-name>
- <url-pattern>/</url-pattern>
- </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="" xmlns:mvc="" xmlns:xsi="" xsi:schemaLocation=""> <!-- 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无法找到控制器并把请求分发到控制器。