2011-01-07 17 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

相关问题