CssLinkResourceTransformer不会修改css文件中存在的URL

时间:2016-06-29 13:37:20

标签: css spring-mvc spring-boot

我使用VersionResourceResolver [Spring Boot 1.2.0]进行缓存清除。使用下面的代码,CSS资源获取版本号,但是即使在使用CssLinkResourceTransformer之后,css文件中的URL也无法获得版本。

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

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

.navbar-header {
    height: 58px;
    background: transparent url("../images/logo-bg.png") no-repeat left center;
}

GET https://localhost:8443/myapp/css/layout-4a5a9811a2a00e7da9b9c0f0fcfbd343.css
GET https://localhost:8443/myapp/images/logo-bg.png

我在这里失踪了什么?如何在CSS文件中对URL进行版本控制。感谢

修改 对不起,我应该已经提供了 addResourceHandlers 方法的完整代码,我现在已经拥有了它。请在下面找到相同的,

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/css/**")
            .addResourceLocations("/css/")
            .resourceChain(true)
            .addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"))
            .addTransformer(new CssLinkResourceTransformer())
            .addTransformer(new AppCacheManifestTransformer());

    registry.addResourceHandler("/fonts/**")
            .addResourceLocations("/fonts/")
            .resourceChain(true)
            .addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"))
            .addTransformer(new AppCacheManifestTransformer());

    registry.addResourceHandler("/html/**")
            .addResourceLocations("/html/");

    registry.addResourceHandler("/images/**")
            .addResourceLocations("/images/")
            .resourceChain(true)
            .addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"))
            .addTransformer(new AppCacheManifestTransformer());

    registry.addResourceHandler("/js/**")
            .addResourceLocations("/js/")
            .resourceChain(true)
            .addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"))
            .addTransformer(new AppCacheManifestTransformer());
}

1 个答案:

答案 0 :(得分:2)

您的配置仅映射与模式"/css/**"匹配且"/images/logo-bg.png"不匹配的资源。

您可以尝试以下方法吗?

registry.addResourceHandler("/css/**", "/images/**")
        .addResourceLocations("/css/", "/images/")

请注意,要相互协作,应在唯一的资源处理程序中声明资源转换器/解析器。