Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java spring.resources.chain.strategy.content.enabled仅用于.css文件_Java_Spring_Spring Boot - Fatal编程技术网

Java spring.resources.chain.strategy.content.enabled仅用于.css文件

Java spring.resources.chain.strategy.content.enabled仅用于.css文件,java,spring,spring-boot,Java,Spring,Spring Boot,我正在尝试使用Thymeleaf作为视图引擎,在Spring启动应用程序中为JavaScript和CSS文件启用ContentVersionStrategy 在a中,我已将以下内容添加到应用程序属性文件中: # add classpath:/custom/ as a possible location spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/

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

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

# 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文件

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

@配置
公共类WebConfiguration实现WebMVCConfiguer
{
私有静态最终字符串[]类路径\资源\位置={
“类路径:/META-INF/resources/”,“类路径:/resources/”,
“类路径:/static/”、“类路径:/public/”、“类路径:/custom/”};
@凌驾
public void addResourceHandlers(ResourceHandlerRegistry注册表)
{
registry.addResourceHandler(“/**”).addResourceLocations(类路径\u资源\u位置)
.resourceChain(真实)
.addResolver(新版本ResourceResolver().addContentVersionStrategy(“/**”);
}
}
但这不适用于.css文件或.js

我试过SpringBoot2.1.4.RELEASE、2.2.0.M2、2.2.0.BUILD-SNAPSHOT、Java11和Java12,以防有任何差异

我的属性文件方法似乎不适用于.js文件的任何原因


我的
webmvcconfiguer
实现是否有任何明显的错误,这意味着它既不适用于.css也不适用于.js?

因此,在Spring中挖掘了5个小时,试图找出为什么没有生成javascript文件的内容哈希值,我发现了css和js链接之间的差异:


测试页
文本页

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

还发现手动执行时缺少的位:

@配置
公共类WebConfiguration实现WebMVCConfiguer
{
私有静态最终字符串[]类路径\资源\位置={
“类路径:/META-INF/resources/”,“类路径:/resources/”,
“类路径:/static/”、“类路径:/public/”、“类路径:/custom/”};
@凌驾
public void addResourceHandlers(ResourceHandlerRegistry注册表)
{
registry.addResourceHandler(“/**”).addResourceLocations(类路径\u资源\u位置)
.resourceChain(真实)
.addResolver(新版本ResourceResolver().addContentVersionStrategy(“/**”);
}
@豆子
公共资源URL编码筛选器资源URL编码筛选器(){
返回新的ResourceUrlEncodingFilter();
}
}

我需要添加
ResourceUrlEncodingFilter
bean。

为了让未来的读者清楚,不需要手动步骤,只需添加
spring.resources.chain.strategy.content.enabled=true-spring.resources.chain.strategy.content.path=/**
应用程序中。properties
工作(使用spring Boot 2.2.6测试)