将default-servlet-handler放在Spring MVC配置中的位置

gig*_*dot 12 java spring-mvc

在我看来web.xml,默认的servlet映射,即/映射到Spring调度程序.在我的Spring调度程序配置中,我有DefaultAnnotationHandlerMapping,ControllerClassNameHandlerMapping并且AnnotationMethodHandlerAdapter允许我通过其类名或@Requestmapping注释将url映射到控制器.但是,Web根目录下有一些静态资源,我也希望spring调度程序使用默认的servlet来提供服务.根据Spring文档,这可以使用<mvc:default-servlet-handler/>tag 完成.

在下面的配置中,我标记了4个可以插入此标记的候选位置.将标记插入不同的位置会导致调度程序的行为方式如下:

情况1:如果我将其插入位置1,调度程序将无法再通过@RequestMapping和控制器类名称处理映射,但它将正常提供静态内容.

Cas 2,3:它能够处理@RequestMapping和控制器类名称的映射,以及如果无法成功完成其他映射,则提供静态内容.

情况4:它无法提供静态内容. 删除注意:这是一个实现控制器时的错误,该控制器具有映射到/**但在控制器类名上没​​有显式请求映射的方法.

因此,案例23是可取的.根​​据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>
Run Code Online (Sandbox Code Playgroud)

gig*_*dot 8

默认情况下,春台的订单的价值HandlerMappingInteger.MAX_VALUE赋予最低的优先顺序.第一次加载调度程序配置时,DispatcherServlet将使用此值对列表进行排序HandlerMapping.

如果未设置order的显式值,则所有处理程序映射对象将具有相同的顺序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>
Run Code Online (Sandbox Code Playgroud)