备忘:在web.xml中使用org.springframework.js.resource.ResourceServlet的配置对静态资源的访问
2011-06-08 18:21
1011 查看
查询了org.springframework.js.resource.ResourceServlet的源程序,发现需要使用这个servlet类时应该可以对以下这些参数进行设置: gzipEnabled allowedResourcePaths compressedMimeTypes jarPathPrefix cacheTimeout 我们项目中的使用: 1)将静态内容放入一个单独的project的META-INF目录下,此project将生成jar供其它project调用 2)在web项目的web.xml加入以下配置: <servlet> <servlet-name>Resource Servlet</servlet-name> <servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class> <init-param> <param-name>allowedResourcePaths</param-name> <param-value> /**/*.css, /**/*.gif, /**/*.ico, /**/*.jpeg, /**/*.jpg, /**/*.js, /**/*.png, /**/*.html, META-INF/**/*.css, META-INF/**/*.gif, META-INF/**/*.ico, META-INF/**/*.jpeg, META-INF/**/*.jpg, META-INF/**/*.js, META-INF/**/*.png, META-INF/**/*.html </param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Resource Servlet</servlet-name> <url-pattern>/resources/*</url-pattern> </servlet-mapping> 3)这样在访问静态页面 META-INF/html/xx.html 时可以通过http://<host>/<context path>/resource/html/xx.html来访问此页面了 4)当想简化以上URL,把resource/html/改为html/时,我加了以下配置,主要是加了 jarPathPrefix 的参数: <servlet> <servlet-name>HTML Resource Servlet</servlet-name> <servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class> <init-param> <param-name>allowedResourcePaths</param-name> <param-value> /**/*.html, META-INF/**/*.html </param-value> </init-param> <init-param> <param-name>jarPathPrefix </param-name> <param-value>META-INF/html</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>HTML Resource Servlet</servlet-name> <url-pattern>/html/*</url-pattern> </servlet-mapping> 参考自:http://www.docjar.com/html/api/org/springframework/js/resource/ResourceServlet.java.html 附上有关参数设置的程序: 383 /** 384 * Set whether to apply gzip compression to resources if the requesting client supports it. 385 */ 386 public void setGzipEnabled(boolean gzipEnabled) { 387 this.gzipEnabled = gzipEnabled; 388 } 389 390 /** 391 * Set allowed resources as an comma separated String of URL patterns, e.g. "META-INF/** /*.js", The paths may be 392 * any Ant-style pattern parsable by AntPathMatcher. 393 * 394 * @see AntPathMatcher 395 */ 396 public void setAllowedResourcePaths(String allowedResourcePaths) { 397 this.allowedResourcePaths = new HashSet(Arrays.asList(StringUtils.tokenizeToStringArray(allowedResourcePaths, 398 ",", true, true))); 399 } 400 401 /** 402 * Set comma separated MIME types that should have gzip compression applied. Typically, gzip compression is only 403 * useful for text based content. Ant-style patterns are supported, e.g. "text/*". 404 * 405 * @see AntPathMatcher 406 */ 407 public void setCompressedMimeTypes(String compressedMimeTypes) { 408 this.compressedMimeTypes = new HashSet(Arrays.asList(StringUtils.tokenizeToStringArray(compressedMimeTypes, 409 ",", true, true))); 410 } 411 412 /** 413 * Set the default path prefix to apply to resources being served from jar files. Default is "META-INF". 414 */ 415 public void setJarPathPrefix(String jarPathPrefix) { 416 this.jarPathPrefix = jarPathPrefix; 417 } 418 419 /** 420 * Set the number of seconds resources should be cached by the client. Zero disables caching. Default is one year. 421 */ 422 public void setCacheTimeout(int cacheTimeout) { 423 this.cacheTimeout = cacheTimeout; 424 }
相关文章推荐
- SpringMvc 访问静态资源 出现[org.springframework.web.servlet.PageNotFound]Request method 'GET' not supported
- web.xml中配置访问资源URL<url-pattern>/</url-pattern>时无法访问.html、.jsp、.js、.css等静态资源时的解决方案
- web.xml中配置访问资源URL<url-pattern>/</url-pattern>时无法访问.html、.jsp、.js、.css等静态资源时的解决方案
- 静态资源[org.springframework.web.servlet.PageNotFound]
- web.xml中配置访问资源URL<url-pattern>/</url-pattern>时无法访问.html、.jsp、.js、.css等静态资源时的解决方案
- Spring MVC集成thymeleaf时提示:defined in ServletContext resource [/WEB-INF/SrpingMVCTest-servlet.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException
- spring mvc框架 web.xml配置文件 Could not open ServletContext resource [/WEB-INF/classes/spring-servlet.xml] 错误
- Idea 添加完项目以后自动生成的web.xml报错 'org.springframework.web.servlet.DispatcherServlet' is not assignable to
- spring MVC关于org.springframework.web.servlet.handler.SimpleUrlHandlerMapping的使用
- Idea 添加完项目以后自动生成的web.xml报错 'org.springframework.web.servlet.DispatcherServlet' is not assignable to
- web.xml里配置servlet,访问找不到资源的解决方法
- Idea 添加完项目以后自动生成的web.xml报错 'org.springframework.web.servlet.DispatcherServlet' is not assignable to
- Idea 添加完项目以后自动生成的web.xml报错 'org.springframework.web.servlet.DispatcherServlet' is not assignable to
- 大数据WEB阶段 Servlet配置优先级 , Spring容器设置对静态资源放行
- Idea 添加完项目以后自动生成的web.xml报错 'org.springframework.web.servlet.DispatcherServlet' is not assignable to
- java报错-->Error creating bean with name 'org.springframework.web.servlet.resource.ResourceHttpRequest
- SpringMVC配置拦截器出现500错误:Error creating bean with name 'org.springframework.web.servlet.handler.Mappe
- 160329(一)、在web.xml文件里配置org.springframework.web.context.ContextLoaderListener
- springmvc访问静态资源的springmvc.xml配置
- web.xml配置详解 org.springframework.web.context.ContextLoaderListener