Spring参考手册 mvc:resources

This tag allows static resource requests following a particular URL pattern to be served by a ResourceHttpRequestHandler from any of a list of Resource locations.
这个标签允许ResourceHttpRequestHandler从资源列表的位置根据指定的url模板请求静态资源。
This provides a convenient way to serve static resources from locations other than the web application root, including locations on the classpath.
他提供了一个方便的途径来从根目录甚至classpath内读取静态资源
The cache-period property may be used to set far future expiration headers (1 year is the recommendation of optimization tools such as Page Speed and YSlow) so that they will be more efficiently utilized by the client.
cache-period属性可以设置很长的缓存时间
The handler also properly evaluates the Last-Modifiedheader (if present) so that a 304 status code will be returned as appropriate, avoiding unnecessary overhead for resources that are already cached by the client.
ResourceHttpRequestHandler 根据Last-Modifiedheader (如果设置了)会返回304状态码,避免重复返回以缓存的资源。
For example, to serve resource requests with a URL pattern of /resources/** from a public-resources directory within the web application root, the tag would be used as follows:
例如,通过/resources/**访问根目录下public-resources文件夹下的资源,标签可以像下边这么写:
<mvc:resources mapping="/resources/**" location="/public-resources/"/>

To serve these resources with a 1-year future expiration to ensure maximum use of the browser cache and a reduction in HTTP requests made by the browser:
设置一年的缓存时间以最大化的利用浏览器缓存和减少服务器压力:
<mvc:resources mapping="/resources/**" location="/public-resources/" cache-period="31556926"/>

The mapping attribute must be an Ant pattern that can be used by SimpleUrlHandlerMapping, and the location attribute must specify one or more valid resource directory locations.
mapping属性必须是可以被SimpleUrlHandlerMapping使用的路径,location属性需指定一个以上正确的目录位置。
Multiple resource locations may be specified using a comma-seperated list of values.
多个资源目录可以使用逗号分隔。
The locations specified will be checked in the specified order for the presence of the resource for any given request.
请求会按配置的顺序依次检测资源目录。
For example, to enable the serving of resources from both the web application root and from a known path of /META-INF/public-web-resources/ in any jar on the classpath, the tag would be specified as:
例如:要想让根目录和classpath下任何jar包的/META-INF/public-web-resources/目录中能够访问,标签可以如下
<mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/public-web-resources/"/>

When serving resources that may change when a new version of the application is deployed, it is recommended that you incorporate a version string into the mapping pattern used to request the resources, so that you may force clients to request the newly deployed version of your application's resources.
当新版本的应用部署后资源可能改变,推荐请求资源时在路径中加入版本戳,这样就可以强制客户端请求新版本的应用资源。
Such a version string can be parameterized and accessed using SpEL so that it may be easily managed in a
single place when deploying new versions.
版本字符串可以使用SpEL 这样当部署新版本是就可以在指定的位置更轻松的管理。
As an example, let's consider an application that uses a performance-optimized custom build (as recommended) of the Dojo JavaScript library in production, and that the build is generally deployed within the web application at a path of /public-resources/dojo/dojo.js.
举个例子,我们考虑一个应用使用Dojo js类库产品中推荐使用而且性能良好的,通常部署在web应用的/public-resources/dojo/dojo.js
Since different parts of Dojo may be incorporated into the custom build for each new version of the application, the client web browsers need to be forced to re-download that custom-built dojo.js resource any time a new version of the application is deployed.
……(翻不出了,上一句也翻的不太对),浏览器需要随时强制下载新版本的dojo.js
A simple way to achieve this would be to manage the version of the application in a properties file, such as:
最简单的方法就是在properties文件中管理版本信息,例如:
application.version=1.0.0
and then to make the properties file's values accessible to SpEL as a bean using the util:properties tag:
使用util:properties标签把properties文件的内容提供给SpEL
<util:properties id="applicationProps" location="/WEB-INF/spring/application.properties"/>
With the application version now accessible via SpEL, we can incorporate this into the use of the resources tag:
通过SpEL得到应用的版本,可以把他加入到用到的resources标签:
<mvc:resources mapping="/resources-#{applicationProps['application.version']}/**" location="/public-resources/"
and finally, to request the resource with the proper URL, we can take advantage of the Spring JSP tags:
最后,可以使用spring jsp标签表示请求资源的url:
<spring:eval expression="@applicationProps['application.version']" var="applicationVersion"/>
<spring:url value="/resources-{applicationVersion}" var="resourceUrl">
<spring:param name="applicationVersion" value="${applicationVersion}"/>
</spring:url>
<script src="${resourceUrl}/dojo/dojo.js" type="text/javascript"> </script>

猜你喜欢

转载自squall140.iteye.com/blog/1538797