java - spring.resources.chain.strategy.content.enabled 仅适用于 .css 文件

标签 java spring spring-boot

我正在尝试使用 Thymeleaf 作为 View 引擎,在 Spring Boot 应用程序中为 JavaScript 和 CSS 文件启用 ContentVersionStrategy。

sample project中我已将以下内容添加到应用程序属性文件中:

# add classpath:/custom/ as a possible location
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/custom/

# enable ContentVersionStrategy
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**

这适用于应用程序中的 .css 文件,但不适用于 .js 文件。据我所知,它也应该适用于 .js 文件。

我还尝试手动执行此操作,而不仅仅是在属性文件中使用:

@Configuration
public class WebConfiguration implements WebMvcConfigurer
{
    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/", "classpath:/resources/",
            "classpath:/static/", "classpath:/public/", "classpath:/custom/"};

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
        registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS)
                .resourceChain(true)
                .addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"));
    }
}

但这不适用于 .css 文件或 .js。

我尝试过使用 Spring Boot 2.1.4.RELEASE、2.2.0.M2、2.2.0.BUILD-SNAPSHOT、Java 11 和 12,以防出现任何差异。

我的属性文件方法似乎不适用于 .js 文件,有什么原因吗?

我的 WebMvcConfigurer 实现是否存在任何明显错误,这意味着它不适用于 .css 或 .js?

最佳答案

因此,在对 Spring 进行了 5 个小时的挖掘并尝试找出为什么未生成 javascript 文件的内容哈希值之后,我发现了 css 和 js 链接之间的差异:

<!doctype html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Test page</title>

    <link rel="stylesheet" th:href="@{/css/jquery-ui.min.css}"/>
    <link rel="stylesheet" th:href="@{/directory/select2.min.css}"/>
</head>
<body>
    <p>Text page</p>
    <script th:src="@{js/jquery.js}"></script>
    <script th:src="@{js/select2.min.js}"></script>
    <script th:src="@{directory/jquery-ui.min.js}"></script>
</body>
</html>

脚本标签缺少开头的正斜杠。添加它可以解决问题。

还发现了我手动执行此操作所缺少的部分:

@Configuration
public class WebConfiguration implements WebMvcConfigurer
{
    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/", "classpath:/resources/",
            "classpath:/static/", "classpath:/public/", "classpath:/custom/"};

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry)
    {
        registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS)
                .resourceChain(true)
                .addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"));
    }

    @Bean
    public ResourceUrlEncodingFilter resourceUrlEncodingFilter() {
        return new ResourceUrlEncodingFilter();
    }
}

我需要添加 ResourceUrlEncodingFilter bean。

关于java - spring.resources.chain.strategy.content.enabled 仅适用于 .css 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56406594/

相关文章:

java - 该算法拼图代码(USACO)的良好解决方案?

java - 认证前检查Spring-Security中的X509证书吊销状态

java - 如何模拟 Springockito 模拟的行为?

java - 如何将数据传递给作为 MethodInvokingJobDetailFactoryBean 运行的方法?

java - 避免多次检查 != null

java - Spring/Spring Boot 转换 - 如何在 Converters 上建立链?

Java - 将 ArrayList 限制为仅 250 位数字

java - 如何使用 spring-boot 显示 Dropwizard Metrics Servlet?

spring - swagger 2 spring boot 生成 yml 文件

java - 除非我等待接受照片,否则 BitmapFactory.decodeFile 为空