The handler mapping from the mvc:resource override other mappings which defined with annotation

霸气de小男生 提交于 2020-03-03 03:17:45

问题


I am new in spring mvc3,and I am trying to create a simple project to get close to spring mvc3.

Now I meet some problem when I try to server some static resources files.

Because I use the url-pattern (/) in the web.xml:

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

So,when I enter: http://locaohost:8080/spring/res/css/main.css. I will get the 404 error.

From the spring document,I try to use the <mvc:resource location="/res/" mapping="/res/**" />

But if I add this tag in the spring-servlet.xml,I found that,I can get the resources file now,but I can not access other page.

That's to say,I have a controller:

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

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

When I visit: http://locaohost:8080/spring/example/hello,I will get 404 now.

But If I remove the tag: <mvc:resource xxx/>

I can access http://locaohost:8080/spring/example/hello,but I can not get the .css/.js file.

Through debugger in eclipse,I found that when spring init the handerMapping in the method "initHanderMapping" of "DispatchServlet",it created two mapping instance: BeanNameUrlHandlerMapping and SimpleUrlHandlerMapping.

The handelrMap property of the BeanNameUrlHandlerMapping is always empty while the SimpleUrlHandlerMapping always contain the url-matching mappings.

When I add the tag ,its handerMapping property is: {/res/**=org.springframework.web.servlet.resource.ResourceHttpRequestHandler@1120eed}

When I remove the tag,the handelrMapping is :{/example/hello=com.spring.controller.HelloController@1b5438d, /example/hello.*=com.spring.controller.HelloController@1b5438d, /example/hello/=com.spring.controller.HelloController@1b5438d}.

It seems that ,the {/res/**=xxxx} override other mappings {/example/helloxxxxx}

This is the 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>

How to fix it?


回答1:


Try adding <mvc:annotation-driven /> to your context.

<mvc:resource...> overrides the default behavior of spring mvc. If you add <mvc:annotation-driven /> to your spring-servlet.xml, it should forcibly register all required handlers.




回答2:


Better solution:

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

This will specify a precedence order for resources.



来源:https://stackoverflow.com/questions/7910845/the-handler-mapping-from-the-mvcresource-override-other-mappings-which-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!