我正在尝试使用Spring Mvc 4进行资源版本控制。我使用thymeleaf模板引擎。但不适用于以下代码。加载页面时,我在查看页面源代码时看不到新版本的网址。那么我的代码有什么问题?我想念什么?
I am trying resource versioning with Spring Mvc 4.I use thymeleaf template engine.But doesnt work with the following code.When load the page I cant see new version Url when i view the page source.So what's the problem in my code? what am i miss?
@Override public void addResourceHandlers(final ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/theme*//**").addResourceLocations("/resources/static/theme/") .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS)) .resourceChain(false) .addResolver(new VersionResourceResolver().addContentVersionStrategy("/**")) .addTransformer(new CssLinkResourceTransformer()); registry.addResourceHandler("/static*//**").addResourceLocations("/resources/static/") .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS)) .resourceChain(false) .addResolver(new VersionResourceResolver().addContentVersionStrategy("/**")) .addTransformer(new CssLinkResourceTransformer()); registry.addResourceHandler("/static/js*//**").addResourceLocations("/resources/static/js/") .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS)) .resourceChain(false) .addResolver(new VersionResourceResolver().addContentVersionStrategy("/**")) .addTransformer(new CssLinkResourceTransformer()); } @Bean public ResourceUrlEncodingFilter resourceUrlEncodingFilter() { return new ResourceUrlEncodingFilter(); }我在脚本标记中使用了表达式。 th:src = @ {/ resources / static / js / companyList.js}
I am using with expression in script tag. th:src="@{/resources/static/js/companyList.js}"
推荐答案这是我的解决方案。我调试Spring.ServletContextResource类的源代码,创建一个relativeRelative。然后检查资源是否存在。
Here is the my solution.I debug source code of Spring.ServletContextResource class create a relativeRelative.Then check whether resource is exists.
资源位置:/资源/静态/
Resource location : /resources/static/
路径:/static/css/login.css
Path : /static/css/login.css
pathToUse:/ resources / static /static/css/login.css->此资源网址不存在,因此返回null。
pathToUse : /resources/static/static/css/login.css --> this resource url not exists so return null.
ServletContextResource类
@Override public Resource createRelative(String relativePath) { String pathToUse = StringUtils.applyRelativePath(this.path, relativePath); return new ServletContextResource(this.servletContext, pathToUse); }解决方案: 资源位置: / resources / static /
Solution: Resource location : /resources/static/
路径:/css/login.css
Path : /css/login.css
pathToUse:/ resources / static / css / login.css
pathToUse : /resources/static/css/login.css
现在我包括此格式。从路径中删除/资源。
Now I include this format.Remove /resources from path.
th:src = @ {/ css / login.css}
@Override public void addResourceHandlers(final ResourceHandlerRegistry registry) { registry.addResourceHandler("/theme*//**").addResourceLocations("/resources/static/") .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS)) .resourceChain(false) .addResolver(new VersionResourceResolver().addContentVersionStrategy("/**")) .addTransformer(new CssLinkResourceTransformer()); registry.addResourceHandler("/css*//**").addResourceLocations("/resources/static/") .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS)) .resourceChain(false) .addResolver(new VersionResourceResolver().addContentVersionStrategy("/**")) .addTransformer(new CssLinkResourceTransformer()); registry.addResourceHandler("/js*//**").addResourceLocations("/resources/static/") .setCacheControl(CacheControl.maxAge(365, TimeUnit.DAYS)) .resourceChain(false) .addResolver(new VersionResourceResolver().addContentVersionStrategy("/**")) .addTransformer(new CssLinkResourceTransformer()); @Override public void configure(final WebSecurity web) throws Exception { web.ignoring().antMatchers("/theme/**").antMatchers("/js/**").antMatchers("/css/**"); }更多推荐
Spring MVC和Thymeleaf资源版本控制
发布评论