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?