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

转载于:https://www.cnblogs.com/h-c-g/p/10849716.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值