2

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?

hguser
  • 35,079
  • 54
  • 159
  • 293

2 Answers2

3

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.

pap
  • 27,064
  • 6
  • 41
  • 46
  • Can you give me more details? – hguser Oct 27 '11 at 10:46
  • Thank you very much!! It works. But I wonder why the spring document does not tell the reader this point! – hguser Oct 27 '11 at 11:24
  • I agree, it's not apparent. It's hinted at in section 15.2 "The DispatcherServlet" of the spring documentation: "The Spring DispatcherServlet uses special beans to process requests and render the appropriate views. These beans are part of Spring Framework. You can configure them in the WebApplicationContext, just as you configure any other bean. However, for most beans, sensible defaults are provided so you initially do not need to configure them." What it doesn't specify outright is that once you start adding your own configuration, the defaults are ignored. That is more implied. – pap Oct 27 '11 at 14:58
0

Better solution:

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

This will specify a precedence order for resources.

Andrew Feng
  • 1,912
  • 17
  • 20