Spring MVC3.0 开始全面支持REST, 但是在处理静态资源方面一直不太理想,在3.0.4发布之前都是通过上一篇文章中介绍的那两种方式处理,但是在3.0.4版本中有了新的处理方式:
1、在spring mvc的配置文件中增加如下标签:
在spring reference中解释如下:
This tag allows for mapping the DispatcherServlet to "/" (thus overriding the mapping of the container's default Servlet), while still allowing static resource requests to be handled by the container's default Servlet. It configures a DefaultServletHttpHandler with a URL mapping (given a lowest precedence order) of "/**". This handler will forward all requests to the default Servlet. To enable this feature using the default setup, simply include the tag in the form: ....
2、现在如果用这个标签,因为spring官方的http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 还没有更新。所以会报错,有以下方法可以代替:
1)手动将3.0.4版本中的spring-mvc-3.0.xsd添加到本地的xml catalog 中,xsd所在位置:
1、在spring mvc的配置文件中增加如下标签:
<mvc:default-servlet-handler/>
在spring reference中解释如下:
This tag allows for mapping the DispatcherServlet to "/" (thus overriding the mapping of the container's default Servlet), while still allowing static resource requests to be handled by the container's default Servlet. It configures a DefaultServletHttpHandler with a URL mapping (given a lowest precedence order) of "/**". This handler will forward all requests to the default Servlet. To enable this feature using the default setup, simply include the tag in the form: ....
2、现在如果用这个标签,因为spring官方的http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 还没有更新。所以会报错,有以下方法可以代替:
1)手动将3.0.4版本中的spring-mvc-3.0.xsd添加到本地的xml catalog 中,xsd所在位置:
org.springframework.web.servlet-3.0.4.RELEASE.jar\org\springframework\web\servlet\config。
2)用另外的配置方法代替:
<bean class="org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler" />
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<util:map>
<entry key="/**"
value="org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler" />
</util:map>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" />