Spring Boot @EnableWebMvc 与 mvc 配置
注意:
1.小心使用 @EnableWebMvc 注解
根据官方文档,尽量不要使用 @EnableWebMvc 注解,因为它会关闭默认配置。
① 你希望关闭默认配置,自己完全重新实现一个
1 2 3 | @EnableWebMvc @Configuration public class WebConfig implements WebMvcConfigurer { |
② 你希望重写部分配置
1 2 3 | //@EnableWebMvc @Configuration public class WebConfig implements WebMvcConfigurer { |
或者
1 2 3 | @EnableWebMvc @Configuration public class WebConfig extends WebMvcAutoConfiguration { |
2.关于静态资源的映射
有两个属性可以了解下:第一行定义匹配的路由地址,第二行定义该路由相匹配的资源的存放位置
1 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 根据内容进行版本区分,从而避免过期缓存。
1 | resources.chain.strategy.content.enabled= true |
服务端开启该功能,前端模板如何生成一个带 md5版本的链接呢?
① 如果使用的是 Thymeleaf,可以使用 @bean 语法访问 ResourceUrlProvider Bean
1 2 3 | <script type= "application/javascript" th:src= "${@mvcResourceUrlProvider.getForLookupPath('/javascript/test.js')}" > </script> |
② 使用 ControllerAdvice (控制器增强,用于拦截控制器方法)
1 2 3 4 5 6 7 8 9 10 11 | @ControllerAdvice public class ResourceUrlAdvice { @Inject ResourceUrlProvider resourceUrlProvider; @ModelAttribute ( "urls" ) public ResourceUrlProvider urls() { return this .resourceUrlProvider; } } |
这样我们可以使用如下的方式生成版本 url
1 2 3 | <script type= "application/javascript" th:src= "${urls.getForLookupPath('/javascript/test.js')}" > </script> |
③ 添加一个 ResourceUrlEncodingFilter Bean,如果模板引擎的 response 调用了 encodeURL() 方法,那么 url将自动版本化(支持 JSPs, Thymeleaf, FreeMarker and Velocity.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | @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 设置
1 2 | 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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | @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 部分属性配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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 |
6. handler 参数解析器
用于对 handler 方法中的参数进行解析,比如注入 CurrentUser
1 2 3 4 5 6 7 8 9 10 | @Configuration @Order (Ordered.HIGHEST_PRECEDENCE) public class BladeWebMvcConfiguration implements WebMvcConfigurer { @Override public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { argumentResolvers.add( new TokenArgumentResolver()); } } |
参考文章
https://www.mscharhag.com/spring/resource-versioning-with-spring-mvc
https://www.jianshu.com/p/917f9e8a94a6
https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/htmlsingle/#common-application-properties (Spring Boot 默认配置)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
2017-10-02 python 解释器
2017-10-02 python 中的 print 函数与 list函数
2017-10-02 python 中的流程控制语句
2017-10-02 python 中 打印及格式化字符串的相关方法
2017-10-02 python 中 try ...except
2017-10-02 python 中面向对象的概念
2017-10-02 python 多返回值