zoukankan      html  css  js  c++  java
  • SpringBoot——配置类实现WebMvcConfigurer接口来配置拦截器、view-controller、视图解析器等

    目的:为了保留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

  • 相关阅读:
    Mysql探索之索引详解,又能和面试官互扯了~
    POJ 1163
    POJ 1157
    POJ 1143
    POJ 1164
    HDU 2553
    POJ 1321
    POJ 1125
    POJ 2488
    POJ 3083
  • 原文地址:https://www.cnblogs.com/bear7/p/13463873.html
Copyright © 2011-2022 走看看