springboot的WebMvcConfigurer接口的addViewControllers方法

springboot的WebMvcConfigurer接口的addViewControllers方法,第1张

package com.zjh.springbootproject.config;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration//声明这是一个springboot配置类
//实现了webmvc接口就不要使用@EnableWebMvc 是由springboot接管mvc
//因为这个注解会导入这个类DelegatingWebMvcConfiguration 这个类中的  private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();
//这个常量他自己的class实现了webmvc接口 而@EnableWebMvc的DelegatingWebMvcConfiguration继承 WebMvcConfigurationSupport加入会有一个condition条件选择器会在容器中找这个类只要这个类实现了就不会去管你其他的webmvcconfiguer类
//添加一个webmvc接口可以用来实现mvc 视图层相关的自定义配置 会覆盖springboot的默认配置 这相当于是一个mvc作用域的拓展类
//@EnableWebMvc
public class StudyWebMvcConfig implements WebMvcConfigurer {


    //这个方法是用来实现页面跳转控制的 就比如你@RequestMapping("/") 然后方法返回的就是setviewname的值
    //这里配置后就不用再去@Controller实现跳转了
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {

        //因为thymeleaf 他默认配置为html结尾就不需要去添加后缀
        //templates目录是模板引擎默认目录他里面的东西都是存放html的
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index").setViewName("index");
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/889880.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-14
下一篇 2022-05-14

发表评论

登录后才能评论

评论列表(0条)

保存