spring3.0.4 新增加的注解(mvc:resources)
程序员文章站
2022-07-12 08:40:03
...
从spring3.0.3发布以后一直等待spring3.0.4发布,今天终于出来,可以甩掉UrlRewriteFilter。
从spring3 mvc支持rest,丰富的注解,应用起来非常方便,对所有的请求当做rest来处理,这就会带来一个问题,如果是静态资源,如js、css和图片,就会导致无法完成请求。为了绕过DispatcherServlet对这些静态资源的处理,多使用UrlRewriteFilter。
web.xml配置如下
urlrewrite的配置,resources下的静态资源不被DispatcherServlet接收处理
spring3.0.4开始更加方便了,一行配置完成上面的请求,/resources/**映射到ResourceHttpRequestHandler进行处理,location指定静态资源的位置.可以是web application根目录下、jar包里面,这样可以把静态资源压缩到jar包中。cache-period 可以使得静态资源进行web cache
resources could be served from a classpath location such as "classpath:/META-INF/public-web-resources/",
allowing convenient packaging and serving of resources such as a JavaScript library from within jar files.
从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> <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> <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
resources could be served from a classpath location such as "classpath:/META-INF/public-web-resources/",
allowing convenient packaging and serving of resources such as a JavaScript library from within jar files.
<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> <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/**"/>