SpringBoot实现MVC配置的扩展

举报
多米诺的古牌 发表于 2021/07/03 11:50:33 2021/07/03
【摘要】 1.SpringBoot框架MVC配置的扩展SpringBoot框架中SpringMvc扩展主要是应用于:自定义视图解析器、接管一些SpringMvc到视图的跳转、自定义格式化等等需要个性化的配置,那么如何实现呢?只需要写这个配置组件,即随便写个类,一般放在config文件夹下(一般自己建),让这个类实现implements WebMvcConfigurer接口。实现SpringMvc扩展配...

1.SpringBoot框架MVC配置的扩展

SpringBoot框架中SpringMvc扩展主要是应用于:自定义视图解析器、接管一些SpringMvc到视图的跳转、自定义格式化等等需要个性化的配置,那么如何实现呢?只需要写这个配置组件,即随便写个类,一般放在config文件夹下(一般自己建),让这个类实现implements WebMvcConfigurer接口。

实现SpringMvc扩展配置类需要做如下操作:

1.1 添加@Configuration注解让spring将这个类标记成一个配置类

1.2 实现implements WebMvcConfigurer接口

1.3 不能添加@EnableWebMvc注解,如果加入这个注解,会使一些这里一些自定义配置失效

这样就实现了SpringMvc扩展配置类配置,接下来我们只要通过这个类重写他的方法,就可以实现相关配置,比如配置拦截器、配置视图控制、配置个性化格式化方法、配置个性化的视图解析器等等。

1.4 配置视图解析器

SpringMvc扩展配置类支持的视图解析器ContentNegotiatingViewResolver中实现viewResolver接口的方法resolveViewName中获取候选的视图
可以看出在List<View> candidateViews = this.getCandidateViews(viewName, locale, requestedMediaTypes);中已经遍历出了所有候选视图的集合
然后筛选得到最终最符合规则的视图 View bestView = this.getBestView(candidateViews, requestedMediaTypes, attrs);

@Nullable
public View resolveViewName(String viewName, Locale locale) throws Exception {
    RequestAttributes attrs = RequestContextHolder.getRequestAttributes();
    Assert.state(attrs instanceof ServletRequestAttributes, "No current ServletRequestAttributes");
    List<MediaType> requestedMediaTypes = this.getMediaTypes(((ServletRequestAttributes)attrs).getRequest());
    if (requestedMediaTypes != null) {
        List<View> candidateViews = this.getCandidateViews(viewName, locale, requestedMediaTypes);
        View bestView = this.getBestView(candidateViews, requestedMediaTypes, attrs);
        if (bestView != null) {
            return bestView;
        }
    }

    String mediaTypeInfo = this.logger.isDebugEnabled() && requestedMediaTypes != null ? " given " + requestedMediaTypes.toString() : "";
    if (this.useNotAcceptableStatusCode) {
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Using 406 NOT_ACCEPTABLE" + mediaTypeInfo);
        }

        return NOT_ACCEPTABLE_VIEW;
    } else {
        this.logger.debug("View remains unresolved" + mediaTypeInfo);
        return null;
    }
}

查看可以看出如果要配置视图解析器,只需要配置一个类实现viewResolver接口,并将这个类在SrpingMvc扩展配置类中注入到bean中,就可以实现自定义配置视图解析器。

由于请求都是通过DispatcherServlet类进行处理的,可以在protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {...}中验证查看到我们自定义配置的视图解析器。

1.5 其中为什么不能加@EnableWebMvc注解

加入这个注解,会使一些这里一些自定义配置失效(比如跳转)因为点进这个注解可以发现他引入了DelegatingWebMvcConfiguration,而这个类又是继承了WebMvcConfigurationSupport类
而从WebMvcAutoConfiguration自动装配类中可以知道筛选条件@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})遇到这个类他会使自动装配失效,所以得出结论是加入这个注解会使WebMvcAutoConfiguration条件不能满足,导致自定义配置失效。

@Configuration

public class MyConfig implements WebMvcConfigurer {
实现ViewResolver接口的类,可以看做视图解析器
    //注入bean后会在左侧出现注入
    @Bean
    public ViewResolver myViewResolver(){
       return new MyViewResolver();
    }

    public static class MyViewResolver implements ViewResolver{
        //可以写在别的地方 也可以写这种内部静态类 然后通过bean注入
        @Override
        public View resolveViewName(String s, Locale locale) throws Exception {
            return null;
       }
    }
}

1.6 配置视图控制

因为配置了SpringMvc扩展类可以,配置很多自定义配置,其中就包括配置视图控制,只需要重写对应的方法就可以实现这些配置。

1.6.1 重写public void addViewControllers(ViewControllerRegistry registry) {...}方法,添加自定义跳转的视图

1.6.2 在addViewControllers方法中添加例如:registry.addViewController("/test").setViewName("index");设置需要的跳转url和这个地址对应的视图

1.6.3 这时只要访问配置的链接就可以自动跳转到setViewName所对应的链接中。比如:这时访问localhost:8080/test就会自动跳转到配置的index.html页面中

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200