當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
Spring Boot2.0+中,自定义配置类扩展springMVC的功能
生活随笔
收集整理的這篇文章主要介紹了
Spring Boot2.0+中,自定义配置类扩展springMVC的功能
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在spring boot1.0+,我們可以使用WebMvcConfigurerAdapter來(lái)擴(kuò)展springMVC的功能,其中自定義的攔截器并不會(huì)攔截靜態(tài)資源(js、css等)。
@Configuration public class MyMvcConfig extends WebMvcConfigurerAdapter {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {// super.addViewControllers(registry);//瀏覽器發(fā)送 /atguigu 請(qǐng)求來(lái)到 successregistry.addViewController("/atguigu").setViewName("success");}//所有的WebMvcConfigurerAdapter組件都會(huì)一起起作用@Bean //將組件注冊(cè)在容器public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/").setViewName("login");registry.addViewController("/index.html").setViewName("login");registry.addViewController("/main.html").setViewName("dashboard");}//注冊(cè)攔截器@Overridepublic void addInterceptors(InterceptorRegistry registry) {//super.addInterceptors(registry);//靜態(tài)資源; *.css , *.js//SpringBoot已經(jīng)做好了靜態(tài)資源映射registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/index.html","/","/user/login");}};return adapter;}@Beanpublic LocaleResolver localeResolver(){return new MyLocaleResolver();}}在spring boot2.0+以后,WebMvcConfigurerAdapter就過(guò)時(shí)了,目前通過(guò)繼承WebMvcConfigurationSupport類(ps:繼承后好像MVC自動(dòng)配置不生效了)或者實(shí)現(xiàn)WebMvcConfigurer接口來(lái)擴(kuò)展springMVC的功能。然而該版本自定義的攔截器會(huì)攔截靜態(tài)資源,因此在使用spring2.0+時(shí),配置攔截器之后,我們要把靜態(tài)資源的路徑加入到不攔截的路徑之中。
@Configuration public class MyMvcConfig implements WebMvcConfigurer{@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/").setViewName("login");registry.addViewController("/index.html").setViewName("login");registry.addViewController("/main.html").setViewName("dashboard");}@Overridepublic void addInterceptors(InterceptorRegistry registry) {//將靜態(tài)資源排除在攔截之外registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/index.html","/","/user/login","/static/**");}@Beanpublic LocaleResolver localeResolver(){return new MyLocaleResolver();}}轉(zhuǎn)載于:https://www.cnblogs.com/XtsLife/p/10488575.html
總結(jié)
以上是生活随笔為你收集整理的Spring Boot2.0+中,自定义配置类扩展springMVC的功能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: PV、UV、UIP、VV、CPC、CPM
- 下一篇: 网页中JS函数自动执行常用三种方法