WebMvcConfigurerAdapter已过时,替换接口或类
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");
}
}
WebMvcConfigurerAdapter已过时,替换接口或类的更多相关文章
- WebMvcConfigurerAdapter过时替换接口或类
(注意!)WebMvcConfigurerAdapter 在spring 5.0中已经弃用了. 原来的使用方式 @Deprecated public abstract class WebMvcConf ...
- WebMvcConfigurerAdapter已过时
Spring Boot2.0的版本(创建的时候自动选择的这个版本),然后编译器告诉我WebMvcConfigurerAdapter已过时了 @Deprecated public abstract cl ...
- SpringBoot从1.5.1→2.2.4项目加包扫雷三:org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter已过时
@Configuration@Slf4j@PropertySource({"classpath:/config.properties"})public class MyWebApp ...
- java 多线程:Thread类常用方法:setPriority优先级、interrupt中断标记、suspend暂停与唤醒resume(已过时);daemon守护线程
常用方法: boolean isAlive() 测试此线程是否存活. boolean isDaemon() 测试此线程是否为守护程序线程. static void sleep?(long millis ...
- Servlet API遍程常用接口和类
本文主要总结Servlet API遍程常用接口和类 Servlet API http://tomcat.apache.org/tomcat-5.5-doc/servletapi/index.html ...
- JAVA中的数据结构——集合类(线性表:Vector、Stack、LinkedList、set接口;键值对:Hashtable、Map接口<HashMap类、TreeMap类>)
Java的集合可以分为两种,第一种是以数组为代表的线性表,基类是Collection:第二种是以Hashtable为代表的键值对. ... 线性表,基类是Collection: 数组类: person ...
- Servlet基本用法(二)接口和类
一.摘要 本文主要简单介绍开发Servlet需要用到的接口和类. 二.ServletRequest和ServletResponse接口 当客户请求到来时,由容器创建一个ServletRequest对象 ...
- 常见的接口与类 -- Comparator
接口Comparator 1.1 前面我们讲过Java提供了一个用于比较的接口Comparable,提供了一个比较的方法,所有实现该接口的类,都动态的实现了该比较方法.实际上Java中除了比较一个接口 ...
- Servlet基本用法二接口和类
转自:http://www.cnblogs.com/xujian2014/p/4536168.html 一.摘要 本文主要简单介绍开发Servlet需要用到的接口和类. 二.ServletReques ...
随机推荐
- GS环境里面 9999 常用密码的加密后的值
1. Test6530 APTZ5s6vrw1dglqO/63osA== 2. aaaaaa zgnewZXGGoqcPGtNpXTSXQ== 3. cwpass 3Me34S0+zY4xEGUFtz ...
- jquery html 獲取或設置
jquery提供操作html元素的屬性和內容的強大方法. DOM就是獨立于平台和語言的界面,允許程序和腳本動態訪問和改變DOM的內容,結構和樣式. 獲取內容:text(),html(),val(),a ...
- JavaScript限制前端页面用户表单输入
onkeyup事件 onkeyup 事件会在键盘按键被松开时发生. 内容来源:自成e家 1.只能输入数字 <input onkeyup = "value=value.replace(/ ...
- Hibernate事务以及一级缓存02
一. Hibernate中的事务 1. 事务的回顾 1.1 什么是事务(Transaction)(面试重点) 是并发控制的单元,是用户定义的一个操作序列.这些操作要么都做,要么都不做,是一个不可分割的 ...
- Mercurial(Hg)基本操作
Mercurial(Hg)基本操作 来源 https://www.cnblogs.com/gb2013/archive/2012/05/18/Mercurial_Basic.html Mercuria ...
- mycat实现简单的mysql集群负载均衡
什么是mycat呢? 简单理解为一个MySQL中间件,它支持分流.基于心跳的自动故障切换,支持读写分离,支持mysql主从,基于Nio管理线程的高并发… 详见官网:http://www.mycat.i ...
- Git Pull Github and Gitee or Gitlab
GitHub实战系列汇总:http://www.cnblogs.com/dunitian/p/5038719.html 缩略Code:https://www.cnblogs.com/dotnetcra ...
- [NOI2017]泳池——概率DP+线性递推
[NOI2017]泳池 实在没有思路啊~~~ luogu题解 1.差分,转化成至多k的概率减去至多k-1的概率.这样就不用记录“有没有出现k”这个信息了 2.n是1e9,感觉要递推然后利用数列的加速技 ...
- A1053. Path of Equal Weight
Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weight of ...
- 1063. Set Similarity
Given two sets of integers, the similarity of the sets is defined to be Nc/Nt*100%, where Nc is the ...