天天看點

使用@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

<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請求才又能被正确捕捉和處理。代碼如下

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/>      
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/spring-beans-3.0.xsd 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 http://blog.csdn.net/jbgtwang/article/details/7359592