Static Resources
此选项提供了一种方便的方法来基于一组https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/io/Resource.html[Resource
] 位置提供静态资源。
This option provides a convenient way to serve static resources from a list of
Resource
-based locations.
在下一个示例中,给定一个以“/resources”开头的请求,相对路径用于查找和提供相对于 Web 应用程序根目录下的“/public”或“/static”下类路径的静态资源。这些资源将以未来一年的过期时间提供,以确保最大程度地利用浏览器缓存并减少浏览器发出的 HTTP 请求。Last-Modified
信息从 Resource#lastModified
推断得出,以支持使用“Last-Modified
”标头的 HTTP 条件请求。
In the next example, given a request that starts with /resources
, the relative path is
used to find and serve static resources relative to /public
under the web application
root or on the classpath under /static
. The resources are served with a one-year future
expiration to ensure maximum use of the browser cache and a reduction in HTTP requests
made by the browser. The Last-Modified
information is deduced from Resource#lastModified
so that HTTP conditional requests are supported with "Last-Modified"
headers.
以下代码段显示了如何执行此操作:
The following listing shows how to do so:
-
Java
-
Kotlin
-
Xml
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/public", "classpath:/static/")
.setCacheControl(CacheControl.maxAge(Duration.ofDays(365)));
}
}
@Configuration
class WebConfiguration : WebMvcConfigurer {
override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/public", "classpath:/static/")
.setCacheControl(CacheControl.maxAge(Duration.ofDays(365)))
}
}
<mvc:resources mapping="/resources/**" location="/public, classpath:/static/"
cache-period="31556926" />
资源处理程序还支持一组https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/resource/ResourceResolver.html[ResourceResolver
] 实现和https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/resource/ResourceTransformer.html[ResourceTransformer
] 实现,你可用它们来创建一个用于处理优化资源的工具链。
The resource handler also supports a chain of
ResourceResolver
implementations and
ResourceTransformer
implementations,
which you can use to create a toolchain for working with optimized resources.
您可以使用 VersionResourceResolver
针对基于从内容、固定应用程序版本或其他内容计算出的 MD5 哈希的版本化资源 URL。ContentVersionStrategy
(MD5 哈希)是一个不错的选择,但有一些值得注意的例外,例如与模块加载器一起使用的 JavaScript 资源。
You can use the VersionResourceResolver
for versioned resource URLs based on an MD5 hash
computed from the content, a fixed application version, or other. A
ContentVersionStrategy
(MD5 hash) is a good choice — with some notable exceptions, such as
JavaScript resources used with a module loader.
以下示例显示了如何使用 VersionResourceResolver
:
The following example shows how to use VersionResourceResolver
:
-
Java
-
Kotlin
-
Xml
@Configuration
public class VersionedConfiguration implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/public/")
.resourceChain(true)
.addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"));
}
}
@Configuration
class VersionedConfiguration : WebMvcConfigurer {
override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/public/")
.resourceChain(true)
.addResolver(VersionResourceResolver().addContentVersionStrategy("/**"))
}
}
<mvc:resources mapping="/resources/**" location="/public/">
<mvc:resource-chain resource-cache="true">
<mvc:resolvers>
<mvc:version-resolver>
<mvc:content-version-strategy patterns="/**"/>
</mvc:version-resolver>
</mvc:resolvers>
</mvc:resource-chain>
</mvc:resources>
然后,你可以使用 ResourceUrlProvider
重写 URL 并应用解析器和转换器的完整链路——例如,插入版本。MVC 配置提供了一个 ResourceUrlProvider
Bean,以便将其注入到其他组件中。你还可以使用 ResourceUrlEncodingFilter
为 Thymeleaf、JSP、FreeMarker 和其他依赖于 HttpServletResponse#encodeURL
的 URL 标记的组件透明地重写。
You can then use ResourceUrlProvider
to rewrite URLs and apply the full chain of resolvers and
transformers — for example, to insert versions. The MVC configuration provides a ResourceUrlProvider
bean so that it can be injected into others. You can also make the rewrite transparent with the
ResourceUrlEncodingFilter
for Thymeleaf, JSPs, FreeMarker, and others with URL tags that
rely on HttpServletResponse#encodeURL
.
请注意,当同时使用 EncodedResourceResolver
(例如,用于提供 GZipped 或 Brotli 编码的资源)和 VersionResourceResolver
时,你必须按此顺序注册它们。这可确保基于未编码文件始终可靠地计算基于内容的版本。
Note that, when using both EncodedResourceResolver
(for example, for serving gzipped or
brotli-encoded resources) and VersionResourceResolver
, you must register them in this order.
That ensures content-based versions are always computed reliably, based on the unencoded file.
对于 WebJars,推荐使用版本化 URL,例如`/webjars/jquery/1.2.0/jquery.min.js`,并且是使用它们的最有效方法。上述资源位置在 Spring Boot 外部已配置(或可通过`ResourceHandlerRegistry`手动进行配置),并且无需添加`org.webjars:webjars-locator-core`依赖项。
For WebJars, versioned URLs like
/webjars/jquery/1.2.0/jquery.min.js
are the recommended and most efficient way to use them.
The related resource location is configured out of the box with Spring Boot (or can be configured
manually via ResourceHandlerRegistry
) and does not require to add the
org.webjars:webjars-locator-core
dependency.
通过 WebJarsResourceResolver
支持像 /webjars/jquery/jquery.min.js
这样的非版本化 URL,当 org.webjars:webjars-locator-core
库存在类路径中时,该解析器会自动注册,而代价是可能会减慢应用程序启动的类路径扫描。该解析器可以重写 URL 以包含 JAR 的版本,并且还可以匹配不带版本号的传入 URL——例如,从 /webjars/jquery/jquery.min.js
匹配到 /webjars/jquery/1.2.0/jquery.min.js
。
Version-less URLs like /webjars/jquery/jquery.min.js
are supported through the
WebJarsResourceResolver
which is automatically registered when the
org.webjars:webjars-locator-core
library is present on the classpath, at the cost of a
classpath scanning that could slow down application startup. The resolver can re-write URLs to
include the version of the jar and can also match against incoming URLs without versions — for example, from /webjars/jquery/jquery.min.js
to /webjars/jquery/1.2.0/jquery.min.js
.
基于 |
The Java configuration based on |