在使用RESTful URI之前必须扫清对静态资源访问的问题,相信大家以前都用UrlRewrite Filter方式对配置uri pattern的静态资源进行URL rewrite.自从Spring3支持RESTful uri后,最新的Spring3.0.4巧妙的解决了对静态资源的访问,解决了开发时期对静态资源访问的问题.
先说下Spring3.0.4对静态资源访问的原理.通过<resources/>或者<mvc:resources/>元素,把mapping的URI注册到SimpleUrlHandlerMapping的urlMap中,key为mapping的URI pattern值,而value为ResourceHttpRequestHandler,这样就巧妙的把对静态资源的访问由HandlerMapping来得到ResourceHttpRequestHandler来处理返回,所以就支持classpath目录,jar包内静态资源的访问.
配置过程遇到的问题主要还是启动时BeanDefinitionXMLParser认不出<resources/>,<mvc:resources/>元素而抛出的异常.解决的方法一是正确的xmlns,xsi:schemaLocation.如下:
for <resources/>
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<resources mapping="/js/**" location="/js/" />
<resources mapping="/image/**" location="/image/" />
for <mvc:resources/>
<?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: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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/image/**" location="/image/" />
光有这些xml头还不行,需要对http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd查找的问题.她会到classpath中查找,所以只须把org.springframework.web.servlet-3.0.4.RELEASE.jar放到web lib目录下,就能查找到xsd,并不需要在IDE中自定义一个User Specified Entries.
另外需要注意的一点是,不要对SimpleUrlHandlerMapping设置defaultHandler.因为对static uri的defaultHandler就是ResourceHttpRequestHandler,否则无法处理static resources request.
但还有个问题困扰这我没搞明白.我这个web工程已对org.springframework.web.servlet.3.0.4(原码工程导入)工程依赖的,为什么非要把org.springframework.web.servlet-3.0.4.RELEASE.jar放入到web lib下才查找到spring-mvc-3.0.xsd?为什么不到依赖的org.springframework.web.servlet.3.0.4原码工程中查找spring-mvc-3.0.xsd?
分享到:
相关推荐
Spring 3.0.4 手册 CHM版
spring security3.0.4 的acl使用例子,相关的说明在 http://blog.csdn.net/lhx1026/archive/2010/11/22/6027125.aspx
Spring3.0.4所需的全部jar包,亲测可用,省去大家下载的时间。
Spring+MVC+3.0.5+Spring+3.0.5+MyBatis3.0.4全注解实例详解
spring 3.0.4 源码 类文件打成jar包,可以直接丢到项目中
这是一个学习Spring3.0.4的学习手册
spring3.0.4说明,详细介绍了spring3.0.4的特性
org.springframework.aop-3.0.4.RELEASE.jar org.springframework.asm-3.0.4.RELEASE.jar org.springframework.aspects-3.0.4.RELEASE.jar org.springframework.beans-3.0.4.RELEASE.jar org.springframework....
Spring_MVC_3.0.5+Spring_3.0.5+MyBatis3.0.4全注解实例详解三.doc
spring 3.0.4+mybatis 3.2.3 在学习myBatis和spring整合的情况下,需要下载对应的jar包,如果不方便在网上到处收集,可以直接通过下载这个zip包,解决就可以得到整合需要用到的包。方便,快捷。
J2EE框架之一的Sping的Framework包,最新版。 里面还包括官方文档。 由于大小限制,不得不将它分为3部分了。
Jbpm4.4+hibernate3.5.4+spring3.0.4+struts2.1.8整合 超级详细的文档,透彻讲解JBPM与SSH的整合过程
SSH框架Jar包介绍 spring3.0.4+hibernate3.5.5+struts2.2.1
struts2.2.1 + hibernate3.6 +spring3.0.4 整合例子
整合struts2.2.1+spring3.0.4+hibernate3.6选择jar包. 不需要的包不要,这样可以使工程不会很庞大。
Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解
spring3.0.4 struts2.1.8 hibernate3.5.5 整合 所需要的jar文件(2) 由于文件稍大 给你第一个文件的链接 http://download.csdn.net/source/2647320
spring3.0.4 struts2.1.8 hibernate3.5.5 整合 所需要的jar文件(1) 导入就能是使用了 不会有冲突. 前提是你的配置文件正确 由于文件太大 给你第二个文件的链接http://download.csdn.net/source/2647325
完整的3.0.4.RELEASE,dist目录下有所有jar包,docs目录下是javadoc-api(英文原版)和关于spring的规范,projects下则是源代码。 Spring-Reference_zh_CN.rar则来自于CSDN,一份中文版的指导。 出于共享的目的,本...