2011-01-07 19 views
12

在我的web.xml中,默認servlet映射即/被映射到Spring分派器。在我的Spring調度程序配置中,我有DefaultAnnotationHandlerMapping,ControllerClassNameHandlerMappingAnnotationMethodHandlerAdapter,它允許我通過它的類名或它的@Requestmapping註釋將url映射到控制器。然而,web根目錄下有一些靜態資源,我也希望spring調度器使用默認的servlet來提供服務。根據Spring documentation,這可以使用<mvc:default-servlet-handler/>標籤完成。將Spring-MVC配置中的默認servlet處理程序放在哪裏

在下面的配置中,我標記了4個候選位置,可以插入此標記。插入在不同的位置標記會導致調度員不同的表現如下:

案例1:如果我在位置1插入,調度員將不再能夠通過@RequestMapping和控制器類名來處理映射但它通常會爲靜態內容提供服務。

Cas 2,3:如果其他映射無法成功完成,它將能夠處理由@RequestMapping和控制器類名稱進行的映射以及爲靜態內容提供服務。

案例4:它將無法提供靜態內容。刪除注意:這是一個錯誤,當實現一個控制器的方法映射到/**但沒有明確的控制器類名的請求映射。

因此,案例2和是可取的。據Spring文檔,這個標籤配置其優先順序給予最低何必位置事項的處理程序?哪個是放這個標籤的最佳位置?

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

    <context:annotation-config/> 
    <context:component-scan base-package="webapp.controller"/> 
    <!-- Location 1 --> 

    <!-- Enable annotation-based controllers using @Controller annotations --> 
    <bean id="annotationUrlMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> 

    <!-- Location 2 --> 
    <bean id="controllerClassNameHandlerMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> 

    <!-- Location 3 --> 
    <bean id="annotationMethodHandlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> 

    <!-- Location 4 --> 
    <mvc:default-servlet-handler/> 

    <!-- All views are JSPs loaded from /WEB-INF/jsp --> 
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 
     <property name="prefix" value="/WEB-INF/jsp/"/> 
     <property name="suffix" value=".jsp"/> 
    </bean> 
</beans> 

回答

9

默認情況下,彈簧將HandlerMapping的順序值設置爲Integer.MAX_VALUE,該值給出最低的優先順序。第一次加載調度程序配置時,DispatcherServlet將使用此值對HandlerMapping的列表進行排序。

如果沒有設置顯式的順序值,那麼所有的處理程序映射對象將具有相同的順序Integer.MAX_VALUE。因此,在排序之後,處理程序映射的順序將與bean定義的順序保持一致。[這聽起來像是執行調度程序時的錯誤]

因此,如果明確設置了處理程序映射的順序值,那麼將<mvc:default-servlet-handler/>標記置於bean定義的任何位置是安全的。

這裏是例子:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
    <context:annotation-config/> 
    <context:component-scan base-package="webapp.controller"/> 

    <mvc:default-servlet-handler /> 

    <!-- Enable annotation-based controllers using @Controller annotations --> 
    <bean id="annotationUrlMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> 
     <property name="order" value="0" /> 
    </bean> 

    <bean id="controllerClassNameHandlerMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"> 
     <property name="order" value="1" /> 
    </bean> 

    <bean id="annotationMethodHandlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> 

    <!-- All views are JSPs loaded from /WEB-INF/jsp --> 
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 
     <property name="prefix" value="/WEB-INF/jsp/"/> 
     <property name="suffix" value=".jsp"/> 
    </bean> 
</beans> 
2

我認爲這歸結於文檔中的錯誤措辭。

它配置一個DefaultServletHttpRequestHandler"/**"

URL映射(給予最低優先順序),我認爲這意味着你應該給它一個較低的優先順序,並不算春將盡這自動。

我不明白爲什麼把它放在位置4不起作用,但我發現位置4和位置3之間沒有區別 - 處理程序適配器不應該干擾映射優先級。

+0

但你無法通過這個標籤設置此映射的順序。 – gigadot 2011-01-07 11:01:29

相關問題