Spring 来自mvc:resource的处理程序映射将覆盖使用注释定义的其他映射

Spring 来自mvc:resource的处理程序映射将覆盖使用注释定义的其他映射,spring,spring-mvc,Spring,Spring Mvc,我是SpringMVC3的新手,我正在尝试创建一个简单的项目来接近SpringMVC3 现在,我在尝试为一些静态资源文件提供服务器时遇到了一些问题 因为我在web.xml中使用url模式(/): <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servl

我是SpringMVC3的新手,我正在尝试创建一个简单的项目来接近SpringMVC3

现在,我在尝试为一些静态资源文件提供服务器时遇到了一些问题

因为我在web.xml中使用url模式(/):

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
当我访问:将得到404现在

但是如果我删除标签:

我可以访问我无法获取.css/.js文件

通过eclipse中的调试器,我发现spring在“DispatchServlet”的方法“initHanderMapping”中初始化handerMapping时,创建了两个映射实例: BeanNameUrlHandlerMapping和SimpleUrlHandlerMapping

BeanNameUrlHandlerMapping的HandlerMap属性始终为空,而SimpleUrlHandlerMapping始终包含url匹配映射

添加标记时,其handerMapping属性为:
{/res/**=org.springframework.web.servlet.resource。ResourceHttpRequestHandler@1120eed}

移除标记时,HandelMapping是:
{/example/hello=com.spring.controller。HelloController@1b5438d,/example/hello.*=com.spring.controller。HelloController@1b5438d,/example/hello/=com.spring.controller。HelloController@1b5438d}

似乎,
{/res/**=xxxx}
覆盖了其他映射
{/example/helloxxxxx}

这是spring-servlet.xml:

<?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:p="http://www.springframework.org/schema/p" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!--    <mvc:resources location="/res/" mapping="/res/**"></mvc:resources>-->
    <context:component-scan base-package="com.spring.controller" />
    <bean id="viewResolver" class="org.springframework.web.servlet.view.tiles2.TilesViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView" />
    </bean>
    <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/jsp/tile_def.xml</value>
            </list>
        </property>
    </bean>
</beans>

/WEB-INF/jsp/tile_def.xml
如何修复它?

尝试将
添加到您的上下文中

覆盖spring mvc的默认行为。如果将
添加到spring-servlet.xml中,它应该强制注册所有必需的处理程序。

更好的解决方案:

<mvc:resources mapping="/resources/**" location="/resources/" order="-1" />


这将指定资源的优先顺序。

您能告诉我更多详细信息吗?非常感谢!!它起作用了。但是我想知道为什么spring文档没有告诉读者这一点!我同意,这并不明显。spring文档的第15.2节“DispatcherServlet”中暗示了这一点:SpringDispatcherServlet使用特殊的bean来处理请求并呈现适当的视图。这些bean是Spring框架的一部分。您可以在WebApplicationContext中配置它们,就像配置任何其他bean一样。但是,对于大多数bean,都提供了合理的默认值,因此您最初不需要配置它们。“它没有明确指定的是,一旦您开始添加自己的配置,默认值就会被忽略。这更隐含。
<mvc:resources mapping="/resources/**" location="/resources/" order="-1" />