对于最新的稳定版本,请使用 Spring Framework 7.0.6!spring-doc.cadn.net.cn

静态资源

此选项提供了一种方便的方式来从基于Resource的列表位置提供静态资源。spring-doc.cadn.net.cn

在下一个示例中,给定一个以 /resources 开头的请求,相对路径用于在 Web 应用程序根目录下或类路径下的 /public 位置查找并提供静态资源。资源的过期时间设置为一年以后,以确保浏览器缓存的最大使用,并减少浏览器发出的 HTTP 请求。从 Resource#lastModified 推断出 Last-Modified 的信息,以便支持带有 "Last-Modified" 标头的 HTTP 条件请求。spring-doc.cadn.net.cn

以下示例展示了如何使用Java配置来实现:spring-doc.cadn.net.cn

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/resources/**")
				.addResourceLocations("/public", "classpath:/static/")
				.setCacheControl(CacheControl.maxAge(Duration.ofDays(365)));
	}
}
@Configuration
@EnableWebMvc
class WebConfig : WebMvcConfigurer {

	override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
		registry.addResourceHandler("/resources/**")
				.addResourceLocations("/public", "classpath:/static/")
				.setCacheControl(CacheControl.maxAge(Duration.ofDays(365)))
	}
}

以下示例展示了如何在XML中实现相同的配置:spring-doc.cadn.net.cn

<mvc:resources mapping="/resources/**"
	location="/public, classpath:/static/"
	cache-period="31556926" />

资源处理器还支持一系列的 ResourceResolver 实现和 ResourceTransformer 实现, 您可以使用它们来创建一个用于处理优化资源的工具链。spring-doc.cadn.net.cn

您可以使用 VersionResourceResolver 用于基于 MD5 哈希计算的版本化资源 URL,该哈希从内容、固定的应用程序版本或其他内容中计算得出。一个 ContentVersionStrategy(MD5 哈希)是不错的选择——尽管有一些显著的例外,例如与模块加载器一起使用的 JavaScript 资源。spring-doc.cadn.net.cn

以下示例展示了如何在Java配置中使用VersionResourceResolverspring-doc.cadn.net.cn

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/resources/**")
				.addResourceLocations("/public/")
				.resourceChain(true)
				.addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"));
	}
}
@Configuration
@EnableWebMvc
class WebConfig : WebMvcConfigurer {

	override fun addResourceHandlers(registry: ResourceHandlerRegistry) {
		registry.addResourceHandler("/resources/**")
				.addResourceLocations("/public/")
				.resourceChain(true)
				.addResolver(VersionResourceResolver().addContentVersionStrategy("/**"))
	}
}

以下示例展示了如何在XML中实现相同的配置:spring-doc.cadn.net.cn

<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,以便它可以被注入到其他bean中。你还可以通过 ResourceUrlEncodingFilter 使重写对Thymeleaf、JSP、FreeMarker和其他依赖于 HttpServletResponse#encodeURL 的URL标签透明。spring-doc.cadn.net.cn

请注意,当同时使用 EncodedResourceResolver(例如,用于提供gzip或brotli编码的资源)和 VersionResourceResolver 时,您必须按此顺序注册它们。这确保了基于内容的版本始终根据未编码文件可靠地计算。spring-doc.cadn.net.cn

对于 WebJars,带版本的 URL 如 /webjars/jquery/1.2.0/jquery.min.js 是推荐且最有效的方法。 相关的资源位置通过 Spring Boot 默认配置(或可以通过 ResourceHandlerRegistry 手动配置),不需要添加 org.webjars:webjars-locator-core 依赖项。spring-doc.cadn.net.cn

无版本的URL,如/webjars/jquery/jquery.min.js,可以通过WebJarsResourceResolver支持,当org.webjars:webjars-locator-core库存在于类路径中时,会自动注册,但会增加类路径扫描,这可能会减慢应用程序的启动。解析器可以将URL重写为包含jar的版本,并且还可以匹配没有版本的传入URL — 例如,从/webjars/jquery/jquery.min.js/webjars/jquery/1.2.0/jquery.min.jsspring-doc.cadn.net.cn

基于 ResourceHandlerRegistry 的 Java 配置提供了更多选项,用于细粒度控制,例如最后修改行为和优化的资源解析。