是什麼緣由形成這種區別的呢?爲何一開始沒用<mvc:annotation-driven/>的時候能夠,添加了默認servlet以後就不行了呢?
html
------------------------------------------------java
This tag registers the DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter beans that are required for Spring MVC to dispatch requests to Controllers.
這個標籤註冊了Spring MVC分發請求到控制器所必須的DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter實例web
The tag configures those two beans with sensible defaults based on what is present in your classpath.
標籤配置的這2個實例能夠根據classpath中的內容默認提供如下功能:spring
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屬性來指定類型轉換服務實例類。json
2. Support for formatting Number fields using the @NumberFormat annotation
支持@NumberFormat 註解格式化數字類型字段。spring-mvc
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或更高版本)字段mvc
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明確的配置app
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 。ide
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