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:
So,when I enter: http://locaohost:8080/spring/res/css/main.css. I will get the 404 error.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>
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:
When I visit: http://locaohost:8080/spring/example/hello,I will get 404 now.Code:@Controller @RequestMapping("/example") public class HelloController { @RequestMapping("hello") public String hello(Model model){ model.addAttribute("name", "John"); return "spring.example.hello"; } }
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:When I remove the tag,the handelrMapping is :Code:{/res/**=org.springframework.web.servlet.resource.ResourceHttpRequestHandler@1120eed}It seems that ,the {/res/**=xxxx} override other mappings {/example/helloxxxxx}Code:{/example/hello=com.spring.controller.HelloController@1b5438d, /example/hello.*=com.spring.controller.HelloController@1b5438d, /example/hello/=com.spring.controller.HelloController@1b5438d}.
This is the spring-servlet.xml:
How to fix it?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>


Reply With Quote