【问题标题】:The handler mapping from the mvc:resource override other mappings which defined with annotation来自 mvc:resource 的处理程序映射覆盖使用注释定义的其他映射
【发布时间】:2011-10-27 01:39:38
【问题描述】:

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

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

因为我在 web.xml 中使用了 url-pattern (/):

<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>

所以,当我输入:http://locaohost:8080/spring/res/css/main.css。我会收到 404 错误。

从spring文档中,我尝试使用&lt;mvc:resource location="/res/" mapping="/res/**" /&gt;

但是如果我在spring-servlet.xml中添加这个标签,我发现,我现在可以获取资源文件,但是我不能访问其他页面。

也就是说,我有一个控制器:

@Controller
@RequestMapping("/example")
public class HelloController {

    @RequestMapping("hello")
    public String hello(Model model){
        model.addAttribute("name", "John");
        return "spring.example.hello";
    }
}

当我访问时:http://locaohost:8080/spring/example/hello,I 现在会得到 404。

但如果我删除标签:&lt;mvc:resource xxx/&gt;

我可以访问http://locaohost:8080/spring/example/hello,but我无法获取 .css/.js 文件。

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

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

当我添加标签时,它的handerMapping属性是:{/res/**=org.springframework.web.servlet.resource.ResourceHttpRequestHandler@1120eed}

当我删除标签时,handelrMapping 是:{/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>

如何解决?

【问题讨论】:

    标签: spring spring-mvc


    【解决方案1】:

    尝试将&lt;mvc:annotation-driven /&gt; 添加到您的上下文中。

    &lt;mvc:resource...&gt; 覆盖 spring mvc 的默认行为。如果你将&lt;mvc:annotation-driven /&gt; 添加到你的spring-servlet.xml,它应该强制注册所有需要的处理程序。

    【讨论】:

    • 你能告诉我更多细节吗?
    • 非常感谢!!有用。但是不知道为什么spring文档没有告诉读者这一点!
    • 我同意,这并不明显。在 spring 文档的第 15.2 节“DispatcherServlet”中暗示:“Spring DispatcherServlet 使用特殊的 bean 来处理请求并呈现适当的视图。这些 bean 是 Spring Framework 的一部分。您可以在 WebApplicationContext 中配置它们,就像您配置任何其他 bean。但是,对于大多数 bean,提供了合理的默认值,因此您最初不需要配置它们。它没有直接指定的是,一旦您开始添加自己的配置,默认值就会被忽略。这更隐含。
    【解决方案2】:

    更好的解决方案:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-15
      • 2023-02-15
      • 1970-01-01
      • 1970-01-01
      • 2012-02-20
      • 2023-03-22
      • 2014-10-23
      相关资源
      最近更新 更多