ResourceHttpRequestHandler
ResourceHttpRequestHandler
可以使用 ResourceResolvers
和 ResourceTransformers
来优化处理静态资源。处理过程如下:
1 | Request for Resource |
ResourceHttpRequestHandler
的配置
配置方法一:
1 |
|
配置方法二:
1 | <mvc:resources mapping="/resources/**" location="/public-resources/"> |
ResourceResolvers
用于解析资源,将外面请求的 public 路径解析为内部的资源路径。
处理过程
1 | Resource link in a template source file |
配置
1 | <mvc:resources mapping="/resources/**" location="/public/"> |
实现类
实现类 | 作用 |
---|---|
PathResourceResolver | finds resources under configured locations (classpath, file system…) matching to the request path |
CachingResourceResolver | resolves resources from a Cache instance or delegates to the next Resolver in the chain |
GzipResourceResolver | finds variations of a resource with a “.gz” extension when HTTP clients support gzip compression |
VersionResourceResolver | resolves request paths containing a version string, i.e. version information about the resource being requested. This resolver can be useful to set up HTTP caching strategies by changing resources’ URLs as they are updated. |
ResourceTransformers
用于修改资源内容。
实现类
实现类 | 作用 |
---|---|
CssLinkResourceTransformer | modifies links in a CSS file to match the public URL paths that should be exposed to clients |
CachingResourceTransformer | caches the result of transformations in a Cache or delegates to the next Transformer in the chain |
AppCacheManifestTransfomer | helps handling resources within HTML5 AppCache manifests for HTML5 offline applications |
静态资源添加 Hash
1 添加 filter
修改 web.xml
:
1 | <!-- 静态资源 URL 添加 Hash --> |
2 添加 resolver
修改 SpringMVC 的配置文件:
1 | <mvc:resources mapping="/static/**" location="/static/" cache-period="31536000"> |
3 修改 view
修改相应的 JSP
:
1 | <!-- css --> |