注意:

1.小心使用  @EnableWebMvc 注解

根据官方文档,尽量不要使用 @EnableWebMvc 注解,因为它会关闭默认配置。

① 你希望关闭默认配置,自己完全重新实现一个

  1. @EnableWebMvc
  2. @Configuration
  3. public class WebConfig implements WebMvcConfigurer {

② 你希望重写部分配置

  1. //@EnableWebMvc
  2. @Configuration
  3. public class WebConfig implements WebMvcConfigurer {

或者

  1. @EnableWebMvc
  2. @Configuration
  3. public class WebConfig extends WebMvcAutoConfiguration {

2.关于静态资源的映射

有两个属性可以了解下:第一行定义匹配的路由地址,第二行定义该路由相匹配的资源的存放位置

  1. spring.mvc.static-path-pattern=/** # Path pattern used for static resources.
  2. 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. <script type="application/javascript"
  2. th:src="${@mvcResourceUrlProvider.getForLookupPath('/javascript/test.js')}">
  3. </script>

② 使用 ControllerAdvice (控制器增强,用于拦截控制器方法)

  1. @ControllerAdvice
  2. public class ResourceUrlAdvice {
  3.  
  4. @Inject
  5. ResourceUrlProvider resourceUrlProvider;
  6.  
  7. @ModelAttribute("urls")
  8. public ResourceUrlProvider urls() {
  9. return this.resourceUrlProvider;
  10. }
  11. }

这样我们可以使用如下的方式生成版本 url

  1. <script type="application/javascript"
  2. th:src="${urls.getForLookupPath('/javascript/test.js')}">
  3. </script>

③  添加一个 ResourceUrlEncodingFilter  Bean,如果模板引擎的 response 调用了 encodeURL() 方法,那么 url将自动版本化(支持 JSPs, Thymeleaf, FreeMarker and Velocity.)

  1. @Configuration
  2. public class WebConfig implements WebMvcConfigurer {
  3.  
  4. @Override
  5. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  6. VersionResourceResolver versionResourceResolver = new VersionResourceResolver()
  7. .addVersionStrategy(new ContentVersionStrategy(), "/**");
  8. registry.addResourceHandler("/javascript/*.js")
  9. .addResourceLocations("classpath:/static/")
  10. .setCachePeriod(60 * 60 * 24 * 365) /* one year */
  11. .resourceChain(true)
  12. .addResolver(versionResourceResolver);
  13. }
  14.  
  15. @Bean
  16. public ResourceUrlEncodingFilter resourceUrlEncodingFilter() {
  17. return new ResourceUrlEncodingFilter();
  18. }
  19. ...

4.locale 设置

  1. spring.mvc.locale.locale=en_US
  2. 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. @Configuration
  2. public class WebConfig implements WebMvcConfigurer {
  3.  
  4. @Bean
  5. public LocaleResolver localeResolver() {
  6. SessionLocaleResolver slr = new SessionLocaleResolver();
  7. slr.setDefaultLocale(Locale.FRENCH);
  8. return slr;
  9. }
  10.  
  11. @Bean
  12. public LocaleChangeInterceptor localeChangeInterceptor() {
  13. return new LocaleChangeInterceptor();
  14. }
  15.  
  16. @Override
  17. public void addInterceptors(InterceptorRegistry registry) {
  18. registry.addInterceptor(localeChangeInterceptor());
  19. }
  20.  
  21. }

 

5.application.properties 部分属性配置

  1. spring:
  2. thymeleaf:
  3. mode: HTML5
  4. cache: false
  5. suffix: .html
  6. encoding: UTF-8
  7. resources:
  8. chain:
  9. cache: false # Whether to enable caching in the Resource chain.
  10. html-application-cache: true # Whether to enable HTML5 application cache manifest rewriting.
  11. strategy.content.enabled: true # Whether to enable the content Version Strategy.
  12. mvc:
  13. date-format: dd/MM/yyyy
  14. locale: zh_CN
  15. locale-resolver: accept_header

6. handler 参数解析器

用于对 handler 方法中的参数进行解析,比如注入 CurrentUser

  1. @Configuration
  2. @Order(Ordered.HIGHEST_PRECEDENCE)
  3. public class BladeWebMvcConfiguration implements WebMvcConfigurer {
  4.  
  5. @Override
  6. public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
  7. argumentResolvers.add(new TokenArgumentResolver());
  8. }
  9.  
  10. }

  

参考文章


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 默认配置)

Spring Boot @EnableWebMvc 与 mvc 配置的更多相关文章

  1. Spring Boot 2.X(四):Spring Boot 自定义 Web MVC 配置

    0.准备 Spring Boot 不仅提供了相当简单使用的自动配置功能,而且开放了非常自由灵活的配置类.Spring MVC 为我们提供了 WebMvcConfigurationSupport 类和一 ...

  2. Spring Boot2 系列教程(十八)Spring Boot 中自定义 SpringMVC 配置

    用过 Spring Boot 的小伙伴都知道,我们只需要在项目中引入 spring-boot-starter-web 依赖,SpringMVC 的一整套东西就会自动给我们配置好,但是,真实的项目环境比 ...

  3. 【面试普通人VS高手系列】Spring Boot的约定优于配置,你的理解是什么?

    对于Spring Boot约定优于配置这个问题,看看普通人和高手是如何回答的? 普通人的回答: 嗯, 在Spring Boot里面,通过约定优于配置这个思想,可以让我们少写很多的配置, 然后就只需要关 ...

  4. 学记:为spring boot写一个自动配置

    spring boot遵循"约定优于配置"的原则,使用annotation对一些常规的配置项做默认配置,减少或不使用xml配置,让你的项目快速运行起来.spring boot的神奇 ...

  5. Spring Boot 探索系列 - 自动化配置篇

    26. Logging Prev  Part IV. Spring Boot features  Next 26. Logging Spring Boot uses Commons Logging f ...

  6. Spring Boot 2.0 教程 | 配置 Undertow 容器

    欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 文章首发于个人网站 https://ww ...

  7. Spring Boot之实现自动配置

    GITHUB地址:https://github.com/zhangboqing/springboot-learning 一.Spring Boot自动配置原理 自动配置功能是由@SpringBootA ...

  8. Spring Boot实践——用外部配置填充Bean属性的几种方法

    引用:https://blog.csdn.net/qq_17586821/article/details/79802320 spring boot允许我们把配置信息外部化.由此,我们就可以在不同的环境 ...

  9. spring boot(5)-properties参数配置

     application.properties application.properties是spring boot默认的配置文件,spring boot默认会在以下两个路径搜索并加载这个文件 s ...

随机推荐

  1. Unity3D 热更新方案总结

    如何评价腾讯在Unity下的xLua(开源)热更方案? Unity 游戏用XLua的HotFix实现热更原理揭秘 腾讯开源手游热更新方案,Unity3D下的Lua编程 [Unity]基于IL代码注入的 ...

  2. 清北学堂学习总结day1

    上午篇 一.高精度计算: [以下内容先只考虑非负数情况] •高精度加法: 思路:[模拟竖式运算] 注意:[进位] •高精度减法: 思路:[同加法类似,模拟竖式运算,进位变退位] 注意: [结果为负数的 ...

  3. 简易解说拉格朗日对偶(Lagrange duality)

    引言:尝试用最简单易懂的描述解释清楚机器学习中会用到的拉格朗日对偶性知识,非科班出身,如有数学专业博友,望多提意见! 1.原始问题 假设是定义在上的连续可微函数(为什么要求连续可微呢,后面再说,这里不 ...

  4. 20155312 张竞予 Exp4 恶意代码分析

    Exp4 恶意代码分析 目录 基础问题回答 (1)如果在工作中怀疑一台主机上有恶意代码,但只是猜想,所有想监控下系统一天天的到底在干些什么.请设计下你想监控的操作有哪些,用什么方法来监控. (2)如果 ...

  5. [Kubernetes]如何让集群为我们工作?

    前一段时间倒腾k8s的时候,写了一系列的博客,有很多人不理解那样做的意义是什么,为什么要那样做,这篇文章就尝试解释一下,在实际环境中,是如何让集群为我们工作的. 因为只研究了一个月左右的时间,认识难免 ...

  6. aiohttp使用队列

    获取百度的搜索结果,然后把百度的长链接,获取到真实的url import time import aiofiles import aiohttp import asyncio from lxml im ...

  7. Set集合判断对象重复的方法

    Set<User> userSet = new HashSet<>(); User user1= new User("aa","11") ...

  8. Ubuntu 18.04LTS 更新镜像配置

    清华大学开源镜像站:https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/ Ubuntu 的软件源配置文件是 /etc/apt/sources.list.将 ...

  9. c++ ignore用法

    转自  http://blog.sina.com.cn/s/blog_4b3336c50102v45n.html std::cin.ignore() can be called three diffe ...

  10. Python学习(三十五)—— Django之ORM训练专题

    图书管理系统 一.表结构设计 # 书 class Book(models.Model): title = models.CharField(max_length=32) publish_date = ...