Spring MVC和Thymeleaf资源版本控制

编程入门 行业动态 更新时间:2024-10-23 13:39:00
本文介绍了Spring MVC和Thymeleaf资源版本控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用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资源版本控制

本文发布于:2023-11-24 21:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626850.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:版本   资源   Spring   MVC   Thymeleaf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!