小编典典

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

spring-mvc

我是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文件,我尝试使用 <mvc:resource location="/res/" mapping="/res/**" />

但是,如果我在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时,我
现在将得到404。

但是,如果我删除标签: <mvc:resource xxx/>

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

通过eclipse中的调试器,我发现在spring初始化“ 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>

如何解决?


阅读 312

收藏
2020-06-01

共1个答案

小编典典

尝试添加<mvc:annotation-driven />到您的上下文中。

<mvc:resource...>覆盖spring mvc的默认行为。如果添加<mvc:annotation-driven />到spring-
servlet.xml,则它应强制注册所有必需的处理程序。

2020-06-01