博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot2.0+中,自定义配置类扩展springMVC的功能
阅读量:4641 次
发布时间:2019-06-09

本文共 2551 字,大约阅读时间需要 8 分钟。

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

@Configurationpublic 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+时,配置拦截器之后,我们要把静态资源的路径加入到不拦截的路径之中。

@Configurationpublic 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();        } }

转载于:https://www.cnblogs.com/XtsLife/p/10488575.html

你可能感兴趣的文章
java笔记--使用SwingWoker类完成耗时操作
查看>>
Android应用程序后台加载数据
查看>>
2016北京集训测试赛(九)Problem C: 狂飙突进的幻想乡
查看>>
CentOS6.5手动升级gcc4.8.2
查看>>
3.9 java基础总结集合①LIst②Set③Map④泛型⑤Collections
查看>>
Unix和Linux下C语言学习指南
查看>>
linux指令
查看>>
linux下面升级 Python版本并修改yum属性信息
查看>>
局域网内通讯APP
查看>>
Unity Shader 图片流光效果实现(纯计算方式)
查看>>
POJ 2002 Squares
查看>>
Java 内存分配
查看>>
ObjectDataSource控件执行Delete操作时,出现“未能找到带参数的非泛型方法”的解决方案...
查看>>
Ubuntu17.10 React Native 环境搭建
查看>>
Atitit 基于sql编程语言的oo面向对象大规模应用解决方案attilax总结
查看>>
jQuery-2.1.4.min.js:4 Uncaught TypeError: Illegal invocation
查看>>
jvm-监控指令-jdump
查看>>
maven安装与配置
查看>>
偶记:mysql5.7的官方doc也有错误啊:写的是vc runtime 2010,但实际上必须是 vc runtime 2013。坑...
查看>>
费马小定理,欧拉定理,指数循环节
查看>>