springboot的WebMvcConfigurer接口的addViewControllers方法

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");
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值