注意:
1.小心使用 @EnableWebMvc 注解
根据官方文档,尽量不要使用 @EnableWebMvc 注解,因为它会关闭默认配置。
① 你希望关闭默认配置,自己完全重新实现一个
@EnableWebMvc @Configuration public class WebConfig implements WebMvcConfigurer {
② 你希望重写部分配置
//@EnableWebMvc @Configuration public class WebConfig implements WebMvcConfigurer {
或者
@EnableWebMvc @Configuration public class WebConfig extends WebMvcAutoConfiguration {
2.关于静态资源的映射
有两个属性可以了解下:第一行定义匹配的路由地址,第二行定义该路由相匹配的资源的存放位置
spring.mvc.static-path-pattern=/** # Path pattern used for static resources. spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ # Locations of static resources.
举例:文件结构
对于 public, static 等文件夹下的静态文件,可以通过 /css/style.css的形式访问(不需要加前缀 static,比如 /static/css/style.css)
3.内容版本策略
ContentVersionStrategy,底层使用 md5 根据内容进行版本区分,从而避免过期缓存。
resources.chain.strategy.content.enabled=true
服务端开启该功能,前端模板如何生成一个带 md5版本的链接呢?
① 如果使用的是 Thymeleaf,可以使用 @bean 语法访问 ResourceUrlProvider Bean
<script type="application/javascript" th:src="${@mvcResourceUrlProvider.getForLookupPath(‘/javascript/test.js‘)}"> </script>
② 使用 ControllerAdvice
@ControllerAdvice public class ResourceUrlAdvice { @Inject ResourceUrlProvider resourceUrlProvider; @ModelAttribute("urls") public ResourceUrlProvider urls() { return this.resourceUrlProvider; } }
这样我们可以使用如下的方式生成版本 url
<script type="application/javascript" th:src="${urls.getForLookupPath(‘/javascript/test.js‘)}"> </script>
③ 添加一个 ResourceUrlEncodingFilter Bean,如果模板引擎的 response 调用了 encodeURL() 方法,那么 url将自动版本化(支持 JSPs, Thymeleaf, FreeMarker and Velocity.)
@Configuration public class WebConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { VersionResourceResolver versionResourceResolver = new VersionResourceResolver() .addVersionStrategy(new ContentVersionStrategy(), "/**"); registry.addResourceHandler("/javascript/*.js") .addResourceLocations("classpath:/static/") .setCachePeriod(60 * 60 * 24 * 365) /* one year */ .resourceChain(true) .addResolver(versionResourceResolver); } @Bean public ResourceUrlEncodingFilter resourceUrlEncodingFilter() { return new ResourceUrlEncodingFilter(); } ...
4.locale 设置
spring.mvc.locale.locale=en_US spring.mvc.locale.locale-resolver=accept_header # Use the "Accept-Language" header or the configured locale if the header is not set
或者使用参数化的配置(比如 http://localhost:8080/?locale=fr)
@Configuration public class WebConfig implements WebMvcConfigurer { @Bean public LocaleResolver localeResolver() { SessionLocaleResolver slr = new SessionLocaleResolver(); slr.setDefaultLocale(Locale.FRENCH); return slr; } @Bean public LocaleChangeInterceptor localeChangeInterceptor() { return new LocaleChangeInterceptor(); } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(localeChangeInterceptor()); } }
5.application.properties 部分属性配置
spring: thymeleaf: mode: HTML5 cache: false suffix: .html encoding: UTF-8 resources: chain: cache: false # Whether to enable caching in the Resource chain. html-application-cache: true # Whether to enable HTML5 application cache manifest rewriting. strategy.content.enabled: true # Whether to enable the content Version Strategy. mvc: date-format: dd/MM/yyyy locale: zh_CN locale-resolver: accept_header
参考文章
https://www.mscharhag.com/spring/resource-versioning-with-spring-mvc
https://www.jianshu.com/p/917f9e8a94a6
原文地址:https://www.cnblogs.com/lemos/p/9736178.html