Results 1 to 3 of 3

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

  1. #1
    Join Date
    Dec 2009
    Posts
    7

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

    Note:
    This is a cross post at stackoverflow,since I do not get any answer,so I post it here.

    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:

    HTML Code:
    <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:

    Code:
    @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:
    Code:
    {/res/**=org.springframework.web.servlet.resource.ResourceHttpRequestHandler@1120eed}
    When I remove the tag,the handelrMapping is :
    Code:
    {/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:

    HTML Code:
    <?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 [url]http://www.springframework.org/schema/beans/spring-beans-3.0.xsd[/url]
            [url]http://www.springframework.org/schema/mvc[/url] [url]http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd[/url]
        [url]http://www.springframework.org/schema/context[/url] 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?

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Please use the search as this question has been answered numerous times before.

    If you use mvc:resource you get a special HandlerMapping which overrides the 2 defaults... Which leads to not mapping your @Controllers anymore...

    Next to the mvc:resource add mvc:annotation-driven...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Dec 2009
    Posts
    7

    Default

    Thanks,It works now.

    I have googled,but I do not find the solution.

    And I wonder why the spring reference does not tell the reader this point?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •