SprigBoot中的 WebMvcConfigurer与 WebMvcConfigurerAdapter和 WebMvcConfigurationSupport
WebMvcConfigurationAdapter 过时?
在SpringBoot2.0之后的版本中WebMvcConfigurerAdapter过时了,所以我们一般采用的是如下的两种的解决的方法。
(1)继承WebMvcConfigureSupport
出现的问题:静态资源的访问的问题,在静态资源的访问的过程中,如果继承了WebMvconfigureSupport的方法的时候,SpringBoot中的自动的配置会失效。 @ConditionalOnMissingBean({WebMvcConfigurationSupport.class}) 表示的是在WebMvcConfigurationSupport类被注册的时候,SpringBoot的自动的配置会失效,就需要你自己进行配置 我自己的代码
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//静态资源的映射
System.out.println("配置了");
registry.addResourceHandler("/")
.addResourceLocations("classpath:/static/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
配置完成之后就会成功,但是在自己配置拦截器的时候对于相应的资源的拦截也要自己配置,十分的麻烦,所以这种方法是不推荐的。
(2)推荐的方法(实现一个WebMvcConfigurer接口)
配置类如下:
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/index").setViewName("login");
registry.addViewController("/main").setViewName("dashboard");
}
/**
*配置拦截器
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
.excludePathPatterns("/index","/","/user/login","/asserts/**","/webjars/**");
}
// @Override
// public void addResourceHandlers(ResourceHandlerRegistry registry) {
// //静态资源的映射
// System.out.println("配置了");
// registry.addResourceHandler("/")
// .addResourceLocations("classpath:/static/");
// registry.addResourceHandler("/webjars/**")
// .addResourceLocations("classpath:/META-INF/resources/webjars/");
// }
/**
* 注意配置的方法的名字
* @return
*/
@Bean
public MyLocaleResolver localeResolver() {
return new MyLocaleResolver();
}
}
不用自己去配置静态资源的映射,配置拦截器的时候,只需要将自己的访问的静态的路径不让拦截器拦截即可
https://msd.misuland.com/pd/3107373619924174722
SprigBoot中的 WebMvcConfigurer与 WebMvcConfigurerAdapter和 WebMvcConfigurationSupport的更多相关文章
- spring boot springMVC扩展配置 。WebMvcConfigurer ,WebMvcConfigurerAdapter
摘要: 在spring boot中 MVC这部分也有默认自动配置,也就是说我们不用做任何配置,那么也是OK的,这个配置类就是 WebMvcAutoConfiguration,但是也时候我们想设置自己的 ...
- WebMvcConfigurationSupport 避坑指南
通过返回WebMvcConfigurationSupport 的方式, 默认会覆盖 Spring boot的自动配置, 导致配置失效静态资源无法访问:但是在WebMvcConfigurationadp ...
- @EnableWebMvc,WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter区别
@EnableWebMvc是什么 直接看源码,@EnableWebMvc实际上引入一个DelegatingWebMvcConfiguration. @Retention(RetentionPolicy ...
- Spring boot 梳理 -@SpringBootApplication、@EnableAutoConfiguration与(@EnableWebMVC、WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter)
@EnableWebMvc=继承DelegatingWebMvcConfiguration=继承WebMvcConfigurationSupport 直接看源码,@EnableWebMvc实际上引入一 ...
- WebMvcConfigurationSupport与WebMvcConfigurer的关系
大家从网上及源码注释上查到的解释是,在spring中配置WebMvc时有两种方法,一种是继承WebMvcConfigurationSupport,重写里面相应的方法,还有一种是继承WebMvcConf ...
- Springboot中WebMvcConfigurer接口详解
Springboot 使用越来越多,企业的基本框架,到Springcloud分布式,可以说无论面试还是平常技术学习,一说到spring几乎就就代替了Java,可以说spring,springboot的 ...
- SpringBoot Web开发(3) WebMvcConfigurerAdapter过期替代方案
springboot2.0中 WebMvcConfigurerAdapter过期替代方案 最近在学习尚硅谷的<springboot核心技术篇>,项目中用到SpringMVC的自动配置和扩展 ...
- Spring Boot2 系列教程(十八)Spring Boot 中自定义 SpringMVC 配置
用过 Spring Boot 的小伙伴都知道,我们只需要在项目中引入 spring-boot-starter-web 依赖,SpringMVC 的一整套东西就会自动给我们配置好,但是,真实的项目环境比 ...
- 在SpringBoot项目中添加logback的MDC
在SpringBoot项目中添加logback的MDC 先看下MDC是什么 Mapped Diagnostic Context,用于打LOG时跟踪一个“会话“.一个”事务“.举例,有一个web ...
随机推荐
- AtCoder Beginner Contest 132 E - Hopscotch Addict
bfs 位置+状态 just need to calculate min value(only it is useful), so O(1*x) 挺有趣的一道题... #include <cst ...
- 如何使用webpack 打包图片
最近在学习vue,需要用到webpack打包css,在webpack中文网https://www.webpackjs.com/里只有css的打包配置, 在编写css样式时,因为要引入 背景图片,打包时 ...
- 【JZOJ6353】给(ca)
description analysis 很妙的\(DP\) 设\(f[i][j]\)表示已经放了\(i\)个叶子节点.根到当前节点走了\(j\)步向左的方案数 考虑调整\(DP\)方式,钦定伸出左儿 ...
- Idea jsp页面乱码
jsp页面中添加 <%@ page language="java" contentType="text/html; charset=utf-8" page ...
- android gradle 和gradle plugin
android gradle 和gradle plugin 1.安装完AS3.5.2创建完项目一运行,报了如下错误 Error:Could not find com.android.tools.bui ...
- SpringBoot_05_ssm拦截器和默认欢迎页面的设置
1.在springBoot下通过使用拦截器完成在没有登陆的前提下,不允许访问其他资源 编写拦截器,要实现HandlerInterceptor @Component public class UserI ...
- clientHeight / scrollHeight / offsetHeight 等属性的区别图
网页(内容)可见区域宽:document.body.clientWidth 网页(内容)可见区域高:document.body.clientHeight 即页面浏览器中可以看到内容的这个区域的高度,一 ...
- 阿里云CentOs7上安装GitLab
一.安装 基本上可以根据官网的教程来安装:https://www.gitlab.com.cn/installation/#centos-7 只不过我们暂时没有邮件服务器,所以postfix没有安装. ...
- SpringBoot--外部配置
常见的SpringBoot外部配置有常规属性配置.类型安全的配置.日志配置.Profile配置 一.常规属性配置 在spring中,注入properties中的配置值时,需要两步: 通过注解@Prop ...
- Sequelize
连接数据库 const DB = require('sequelize') // 连接数据库 const connect = new DB('xjg', 'root', 'root', { host: ...