`
kensunhu
  • 浏览: 16798 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring3.0.4MVC的resources使用之旅

阅读更多

   在使用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?

     

4
2
分享到:
评论
21 楼 java_and_music 2013-05-10  
还是会报错:
Description	Resource	Path	Location	Type
cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:resources'.	mac-app-servlet.xml	/CheBack/WebRoot/WEB-INF	line 99	XML Problem

20 楼 java_and_music 2013-05-10  
我用的是
org.springframework.web.servlet-3.1.1.RELEASE.jar
和org.springframework.web-3.1.1.RELEASE.jar
	<!-- MessageResolve -->
	<bean id="messageSource"
		class="org.springframework.context.support.ResourceBundleMessageSource">
		<property name="basenames">
			<list>
				<value>res.messages</value>
			</list>
		</property>
	</bean>
	<bean id="localeResolver"
		class="org.springframework.web.servlet.i18n.CookieLocaleResolver"
		p:cookieName="clientLanguage" p:cookieMaxAge="10000" p:cookiePath="/"
		p:defaultLocale="en" />
	<mvc:interceptors>
		<bean id="SM" class="org.antonyframework.web.interceptor.LoggerInterceptor" />
		<bean id="localeChangeInterceptor"
			class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />

	</mvc:interceptors>

	<mvc:resources mapping="/uploads/**" location="/uploads/"
		cache-period="31536000" />
	<mvc:resources mapping="/jquery/**" location="/jquery/"
		cache-period="31536000" />
	
19 楼 rumcoke 2013-01-07  
stackoverflow
awdxzc 写道
我用maven2, 它是关联到.jar包的,找不到XSD,配置了一个User Specified Entries后xml文件不提示错误了,但是启动服务器还是报同样的错误。
cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element '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/mvc111">

	<!-- Configures the @Controller programming model -->
	<mvc:annotation-driven/>

	
	<mvc:resources mapping="/scripts/**" location="/scripts/"/>
	<mvc:resources mapping="/styles/**" location="/styles/"/>
	<mvc:resources mapping="/assets/**" location="/assets/"/>


http://www.springframework.org/schema/mvc111是我配置的User Specified Entries

看主贴的项目目录截图 可以尝试把webapp目录设置为源码目录 我自己这样做以后就没再遇到过错误提示 这是从stackoverflow上搜来的一个解决办法 (别人的汗水呵呵)
18 楼 bingguo 2012-03-16  
bingguo 写道
lnzxl 写道
只须把org.springframework.web.servlet-3.0.4.RELEASE.jar放到web lib目录下,就能查找到xsd


但是org.springframework.web.servlet-3.0.4.RELEASE.jar文件中并没有xsd文件啊,而且我这个jar本来就lib文件夹下的,也还是出现cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:resources'。


为什么我的还是报错,描述不到呐,我的是3.0.5的,
17 楼 bingguo 2012-03-16  
lnzxl 写道
只须把org.springframework.web.servlet-3.0.4.RELEASE.jar放到web lib目录下,就能查找到xsd


但是org.springframework.web.servlet-3.0.4.RELEASE.jar文件中并没有xsd文件啊,而且我这个jar本来就lib文件夹下的,也还是出现cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:resources'。

16 楼 forcer521 2011-03-29  
我的配置代码:

<mvc:resources mapping="/theme/**" location="/theme/" />
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/favicon.ico" location="/favicon.ico" />


目录是可以了,但是那个图标文件死活都找不到呀,总不能弄个根目录映射吧?
15 楼 awdxzc 2010-10-20  
我现在差不多理解了这个标签,
<mvc:resources mapping="/scripts/**" location="/scripts/"/>  

其实就是将页面发现了有引用 "/scripts/**" 的路径引用转到工程的/scripts/目录取资源,所以我的页面设计为了保证能取到资源还是必须要有带工程名的前缀,比方说
<c:url value="/scripts/base.js"/>
.
而这个标签真正的意义可能就是让你的rest风格的URL不受影响正常取到资源并且提供一些不能直接访问的资源的获取方式吧。大家多交流,不知道我的理解是否正确。
14 楼 awdxzc 2010-10-20  
kensunhu 写道
原来你是带应用上下文路径,src带上上下文路径.
jsp页面:
<%
String contextPath = request.getContextPath();
%>
src="<%=contextPath%>/assets/images/mstarlogo.gif"/>
若是template页面:
你就设个全局的上下文变量
src="$!{contextPath}/assets/images/mstarlogo.gif"/>

这样就和mvc:resources 没关系了。
我看了下这个标签它貌似就是只起个替换的作用而已。
如果<mvc:resources mapping="/scripts/**" location="/scripts/"/>  就没意义了
<mvc:resources mapping="/assets/**" location="/scripts/"/>  还能理解它有一定的意义
13 楼 lnzxl 2010-10-08  
找到了,并且好用了,谢谢
12 楼 kensunhu 2010-10-07  
lnzxl 写道
只须把org.springframework.web.servlet-3.0.4.RELEASE.jar放到web lib目录下,就能查找到xsd


但是org.springframework.web.servlet-3.0.4.RELEASE.jar文件中并没有xsd文件啊,而且我这个jar本来就lib文件夹下的,也还是出现cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:resources'。


怎么会没有呢?解压ramework.web.servlet-3.0.4.RELEASE.jar可以找到ramework.web.servlet-3.0.4.RELEASE.jar\org\springframework\web\servlet\config\spring-mvc-3.0.xsd
11 楼 lnzxl 2010-10-06  
只须把org.springframework.web.servlet-3.0.4.RELEASE.jar放到web lib目录下,就能查找到xsd


但是org.springframework.web.servlet-3.0.4.RELEASE.jar文件中并没有xsd文件啊,而且我这个jar本来就lib文件夹下的,也还是出现cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:resources'。
10 楼 kensunhu 2010-09-30  
原来你是带应用上下文路径,src带上上下文路径.
jsp页面:
<%
String contextPath = request.getContextPath();
%>
src="<%=contextPath%>/assets/images/mstarlogo.gif"/>
若是template页面:
你就设个全局的上下文变量
src="$!{contextPath}/assets/images/mstarlogo.gif"/>
9 楼 awdxzc 2010-09-28  
kensunhu 写道
你用http://localhost:8080/assets/images/mstarlogo.gif看能否打开?
你的配置,使用image uri都没问题.


这样肯定打不开的,我是个WEB工程,带工程名,这样可以打开。
http://localhost:8080/{webProjectName}/assets/images/mstarlogo.gif。

所以这样的话,我就不能理解这样的标签使用的意义在哪里,我的页面也得写成相对路径。因为如果是
src="/assets/images/mstarlogo.gif"/>
的话肯定到根目录去了。难道它这个标签就是这样使用的?难道就是要丢到应用服务器的根目录里面去? 请教。
8 楼 kensunhu 2010-09-25  
你用http://localhost:8080/assets/images/mstarlogo.gif看能否打开?
你的配置,使用image uri都没问题.
7 楼 awdxzc 2010-09-24  

<img class="logo" src="/assets/images/mstarlogo.gif"/>

如果spring的mvc:resouces标签没有作用到JSP页面的话,那我这样的写法肯定是错的,这样是应用到根目录了,所以我现在对于这个mvc:resources怎么使用还是不清楚。请教。谢谢。
6 楼 awdxzc 2010-09-24  
放到lib里面不识别。还是报错。我还是使用的配置User Specified Entries的方式解决的。启动也不报错了。User Specified Entries之前配置有问题。但是我还有个问题,mvc:resources 在jsp里面是怎么体现使用的?
<mvc:resources mapping="/assets/**" location="/assets/"/>

<img class="logo" src="/assets/images/mstarlogo.gif"/>

我的目录结构webapp下面有assets文件夹,其下有images,其下也有mstarlogo.gif图片。可是我的URL变动还是取不到固定的图片资源。请教。
5 楼 kensunhu 2010-09-24  
你说对了.去官网下载个吧.
4 楼 awdxzc 2010-09-24  
kensunhu 写道
  但还有个问题困扰这我没搞明白.我这个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?
  
   这是spring3.0.4版在开发环境下的一个bug,必须在运行环境下就没有这个问题,所在用3.0.4的就只好把org.springframework.web.servlet-3.0.4.RELEASE.jar放入到web lib下.


不好意思,我没有找到一个这样的包。没找到下载地址。maven2公共库里面也没有这个jar.
没有org.springframework.web.servlet-3.0.4.RELEASE.jar
只有org.springframework.web-3.0.4.RELEASE.jar
可能是我的包的问题?
3 楼 kensunhu 2010-09-23  
  但还有个问题困扰这我没搞明白.我这个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?
  
   这是spring3.0.4版在开发环境下的一个bug,必须在运行环境下就没有这个问题,所在用3.0.4的就只好把org.springframework.web.servlet-3.0.4.RELEASE.jar放入到web lib下.
2 楼 kensunhu 2010-09-23  
1.xml头还是用我上面贴出来的
2.把org.springframework.web.servlet-3.0.4.RELEASE.jar放入到web lib下.
你试试看,应不会有这样的错误了.
cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:resources'.
这个错误我也遇到过.

另外在开发环境下会产生对resources元素没找到情况,这是3.0.4版的一个bug.
会在3.0.5中更新spring-mvc-3.0.4.xsd.

相关推荐

Global site tag (gtag.js) - Google Analytics