目的:為了保留SpringBoot對SpringMVC自動配置,另外我們還想要做一些自己拓展的功能
如何做擴展?
以配置view-controller實現跳轉為例:
原先在SpringMvc中我們寫view-controller:
<mvc:view-controller path="/hello" view-name="success"/>
在springboot中,我們實現這個功能,需要創建一個配置類(類上加Configuration注解),然后實現WebMvcConfigurer接口(在springboot2以前不是實現WebMvcConfigurer接口,而是繼承WebMvcConfigurerAdapter類)。最后我們需要拓展什么功能,只需要重寫WebMvcConfigurer接口中的默認方法即可。
例如要實現頁面跳轉功能,我們只需要重寫addViewControllers方法。
@Configuration public class MyMvcConfig implements WebMvcConfigurer{ @Override public void addInterceptors(InterceptorRegistry registry) { } @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/xiongjun").setViewName("success"); } }
除了這個,我們還可以在這里拓展配置攔截器、視圖解析器,自定義靜態資源映射目錄等等。。
詳情可參考這篇博客:https://blog.csdn.net/zhangpower1993/article/details/89016503