zoukankan      html  css  js  c++  java
  • Spring Boot2.0+中,自定义配置类扩展springMVC的功能

    在spring boot1.0+,我们可以使用WebMvcConfigurerAdapter来扩展springMVC的功能,其中自定义的拦截器并不会拦截静态资源(js、css等)。

    @Configuration
    public class MyMvcConfig extends WebMvcConfigurerAdapter {
        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
           // super.addViewControllers(registry);
            //浏览器发送 /atguigu 请求来到 success
            registry.addViewController("/atguigu").setViewName("success");
        }
        //所有的WebMvcConfigurerAdapter组件都会一起起作用
        @Bean //将组件注册在容器
        public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
            WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
                @Override
                public void addViewControllers(ViewControllerRegistry registry) {
                    registry.addViewController("/").setViewName("login");
                    registry.addViewController("/index.html").setViewName("login");
                    registry.addViewController("/main.html").setViewName("dashboard");
                }
                //注册拦截器
                @Override
                public void addInterceptors(InterceptorRegistry registry) {
                    //super.addInterceptors(registry);
                    //静态资源;  *.css , *.js
                    //SpringBoot已经做好了静态资源映射
                    registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
                            .excludePathPatterns("/index.html","/","/user/login");
                }
            };
            return adapter;
        }
        @Bean
        public LocaleResolver localeResolver(){
     
            return new MyLocaleResolver();
        }
     
    }
    

    在spring boot2.0+以后,WebMvcConfigurerAdapter就过时了,目前通过继承WebMvcConfigurationSupport类(ps:继承后好像MVC自动配置不生效了)或者实现WebMvcConfigurer接口来扩展springMVC的功能。然而该版本自定义的拦截器会拦截静态资源,因此在使用spring2.0+时,配置拦截器之后,我们要把静态资源的路径加入到不拦截的路径之中。

    @Configuration
    public class MyMvcConfig implements WebMvcConfigurer{
     
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
     
                registry.addViewController("/").setViewName("login");
                registry.addViewController("/index.html").setViewName("login");
                registry.addViewController("/main.html").setViewName("dashboard");
            }
     
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                //将静态资源排除在拦截之外
                registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
                        .excludePathPatterns("/index.html","/","/user/login","/static/**");
            }
            @Bean
            public LocaleResolver localeResolver(){
                return new MyLocaleResolver();
            }
     
    }
    


    作者:关小涛
    学习和分享是博客最大的乐趣,欢迎大家取之所需。
    努力是自己努力的原因,每周天写博客总结工作中的新技能和出现的问题
  • 相关阅读:
    html5中将图片的绝对路径转换成文件对象
    Vue中之nextTick函数源码分析
    javascript中的异步 macrotask 和 microtask 简介
    HTML5可预览多图片ajax上传(使用formData传递数据)
    vue双向绑定的原理及实现双向绑定MVVM源码分析
    理解Vue中的Render渲染函数
    如何实现一个 Virtual DOM 及源码分析
    diff.js 列表对比算法 源码分析
    理解spread运算符与rest参数
    go语言之进阶篇普通变量的方法集
  • 原文地址:https://www.cnblogs.com/XtsLife/p/10488575.html
Copyright © 2011-2022 走看看