WebMvcConfigurerAdapter已经过时,在新版本2.x中被废弃,原因是springboot2.0以后,引用的是spring5.0,而spring5.0取消了WebMvcConfigurerAdapter 

以下WebMvcConfigurerAdapter 比较常用的重写接口

/** 解决跨域问题 **/
public void addCorsMappings(CorsRegistry registry) ;
/** 添加拦截器 **/
void addInterceptors(InterceptorRegistry registry);
/** 这里配置视图解析器 **/
void configureViewResolvers(ViewResolverRegistry registry);
/** 配置内容裁决的一些选项 **/
void configureContentNegotiation(ContentNegotiationConfigurer configurer);
/** 视图跳转控制器 **/
void addViewControllers(ViewControllerRegistry registry);
/** 静态资源处理 **/
void addResourceHandlers(ResourceHandlerRegistry registry);
/** 默认静态资源处理器 **/
void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer);

新的版本解决方案目前有两种
方案1 直接实现WebMvcConfigurer  (推荐)

* <p>{@code @EnableWebMvc}-annotated configuration classes may implement
* this interface to be called back and given a chance to customize the
* default configuration. --默认配置
@Configuration
public class MyWebMvcConfg implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/index").setViewName("index");
}
}

方案2 直接继承WebMvcConfigurationSupport

继承WebMvcConfigurationSupport类,在扩展的类中重写父类的方法即可,但这种方式会有问题的,这种方式会屏蔽Spring Boot的@EnableAutoConfiguration中的设置。这时候启动项目时会发现映射根本没有加载成功,读取不到静态的资源也就是说application.properties中添加配置的映射配置没有起作用,然后我们会想到重写来进行重新映射

@Configuration
public class MyMvcConfig extends WebMvcConfigurationSupport{ @Bean
public WebMvcConfigurationSupport webMvcConfigurationSupport(){
WebMvcConfigurationSupport support = new WebMvcConfigurationSupport(){
@Override
protected void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/main.html").setViewName("dashboard");
// registry.addViewController("/login.html").setViewName("login");
} @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//registry.addResourceHandler("/resources/static/**").addResourceLocations("classpath:/static/");
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/resources/static/");
super.addResourceHandlers(registry);
}
};
return support;
}

或是

@Configuration
public class MyMvcConfig extends WebMvcConfigurationSupport { @Override
protected void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("login");
registry.addViewController("/main.html").setViewName("dashboard");
// registry.addViewController("/login.html").setViewName("login");
}
}

最新文章

  1. .NET跨平台:在mac命令行下用vim手写ASP.NET 5 MVC程序
  2. [C# 基础知识系列]C#中易混淆的知识点
  3. ios设备突破微信小视频6S限制的方法
  4. Ajax获得站点文件内容实例
  5. java package and import
  6. [java学习笔记]java语言基础概述之数组的定义&amp;常见操作(遍历、排序、查找)&amp;二维数组
  7. centos中使用python遇到的几个问题
  8. 242. Valid Anagram(C++)
  9. 绿色mysql启动脚本
  10. js函数变量
  11. Mysql之CentOS初探
  12. Win 10 下 android studio显示 Intel haxm无法安装,以及VT-X和hyper-x的冲突问题
  13. ActiveMQ详解
  14. ss - linux网络工具
  15. html之css选择器学习
  16. [R] t.test()
  17. 第 8 章 容器网络 - 054 - 准备 macvlan 环境
  18. LSTM长短期记忆神经网络模型简介
  19. Microsoft Tech Summit 2017
  20. Mininet入门与实战 3.9参课记录

热门文章

  1. Java的JDK下Hashtable与HashMap的区别
  2. [转帖] select、poll、epoll之间的区别总结[整理] + 知乎大神解答 https://blog.csdn.net/qq546770908/article/details/53082870 不过图都裂了.
  3. git 快捷键
  4. 将ubuntu14.04 从mysql从5.5删除之后安装5.7遇到的一些问题(本篇不讨论热升级)
  5. 关于linux上文件无法正确显示中文的情况解决
  6. [转]ubuntu中查找软件的安装位置
  7. 怎样让DBGrid在按住Shift点鼠标的同时能将连续范围的多行选中?
  8. Mysql 乐观锁
  9. python之OrderedDict类
  10. 学习笔记12之通过ajax动态添加选项