WebMvcConfigurerAdapter已過時,使用WebMvcConfigurationSupport或者WebMvcConfigurer來代替


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");
       }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM