使用@Controller註解爲什麼要配置

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

<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無法找到控制器並把請求分發到控制器。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章