栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot的WebMvcConfigurer接口的addViewControllers方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/872470.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号