在我的web.xml
中,默認servlet映射即/
被映射到Spring分派器。在我的Spring調度程序配置中,我有DefaultAnnotationHandlerMapping
,ControllerClassNameHandlerMapping
和AnnotationMethodHandlerAdapter
,它允許我通過它的類名或它的@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>
但你無法通過這個標籤設置此映射的順序。 – gigadot 2011-01-07 11:01:29