查询了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 }
备忘:在web.xml中使用org.springframework.js.resource.ResourceServlet的配置对静态资源的访问
最新推荐文章于 2022-03-19 13:09:51 发布