【问题标题】:Where to put default-servlet-handler in Spring MVC configuration在 Spring MVC 配置中放置 default-servlet-handler 的位置
【发布时间】:2011-06-05 04:28:03
【问题描述】:

在我的web.xml 中,默认的servlet 映射,即/,被映射到Spring dispatcher。在我的 Spring 调度程序配置中,我有 DefaultAnnotationHandlerMappingControllerClassNameHandlerMappingAnnotationMethodHandlerAdapter,它们允许我通过类名或 @Requestmapping 注释将 url 映射到控制器。但是,在 web 根目录下有一些静态资源,我还希望 spring 调度程序使用默认 servlet 提供服务。根据Spring documentation,这可以使用<mvc:default-servlet-handler/>标签来完成。

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

案例 1:如果我将其插入位置 1,调度程序将不再能够通过 @RequestMapping 和控制器类名称处理映射,但它将正常提供静态内容。

Cas 2, 3 : 可以通过@RequestMapping 和控制器类名来处理映射,如果其他映射不能成功,也可以提供静态内容。

案例 4:它将无法提供静态内容。 删除注意:这是实现具有映射方法的控制器时的错误到/**,但在控制器类名上没​​有明确的请求映射。

因此,Case 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>

【问题讨论】:

    标签: java spring-mvc


    【解决方案1】:

    我认为这是由于文档中的措辞不当造成的。

    它使用"/**" 的 URL 映射(给定最低优先级顺序)配置 DefaultServletHttpRequestHandler

    我认为这意味着您应该给它一个较低的优先顺序,而不是 Spring 会自动执行此操作。

    我不明白为什么将它放在位置 4 不起作用,但我认为位置 4 和位置 3 之间没有区别 - 处理程序适配器不应干扰映射优先级。

    【讨论】:

    • 但是不能通过这个标签来设置这个映射的顺序。
    • @gigadot:该顺序隐含在 bean 定义的顺序中。
    • 你说得对,位置 3 和 4 之间没有区别。我已经更新了问题和答案。 Spring 确实为mvc:default-servlet-handler 标签配置的处理程序映射提供了最低优先级,但如果未设置显式值,它也会为其他处理程序映射提供相同的值。顺便说一句,谢谢您的回复。
    【解决方案2】:

    默认情况下,Spring 将HandlerMapping 的顺序值设置为Integer.MAX_VALUE,这给出了最低的优先顺序。首次加载调度程序配置时,DispatcherServlet 将使用此值对HandlerMapping 的列表进行排序。

    如果 order 的显式值未设置,则所有处理程序映射对象将具有相同的顺序 Integer.MAX_VALUE。因此,在排序之后,处理程序映射的顺序将保持与 bean 定义的顺序相同。 [这听起来像是调度程序实现中的一个错误]

    因此,如果显式设置处理程序映射的顺序值,则可以安全地将&lt;mvc:default-servlet-handler/&gt; 标记放在 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-25
      • 2012-04-09
      • 1970-01-01
      • 1970-01-01
      • 2015-07-06
      • 2014-03-10
      • 2011-05-09
      • 1970-01-01
      相关资源
      最近更新 更多