appcontext中的mvc:resources注解

从spring3 mvc支持rest,丰富的注解,应用起来非常方便,对所有的请求当做rest来处理,这就会带来一个问题,如果是静态资源,如js、css和图片,就会导致无法完成请求。为了绕过DispatcherServlet对这些静态资源的处理,多使用UrlRewriteFilter。
web.xml配置如下
<servlet>
	<servlet-name>starframe</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath*:applicationContext-mvc.xml
		</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
	<servlet-name>starframe</servlet-name>
	<url-pattern>/app/*</url-pattern>
</servlet-mapping>


urlrewrite的配置,resources下的静态资源不被DispatcherServlet接收处理

<filter>
	<filter-name>UrlRewriteFilter</filter-name>
	<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
	<filter-name>UrlRewriteFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>


<urlrewrite default-match-type="wildcard">
	<rule>
		<from>/resources/**</from>
		<to>/resources/$1</to>
	</rule>
	<rule>
		<from>/**</from>
		<to>/app/$1</to>
	</rule>
	<outbound-rule>
		<from>/app/**</from>
		<to>/$1</to>
	</outbound-rule>
</urlrewrite>



spring3.0.4开始更加方便了,一行配置完成上面的请求,/resources/**映射到ResourceHttpRequestHandler进行处理,location指定静态资源的位置.可以是web application根目录下、jar包里面,这样可以把静态资源压缩到jar包中。cache-period 可以使得静态资源进行web cache

<servlet>
	<servlet-name>starframe</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath*:applicationContext-mvc.xml
		</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
	<servlet-name>starframe</servlet-name>
	<url-pattern>/</url-pattern>
</servlet-mapping>



<mvc:resources location="/resources/" mapping="/resources/**"/>

你可能感兴趣的:(spring)