Springboot项目中自定义的实现WebMvcConfigurerAdapter 的类中的重写方法addViewControllers 无法映射,映射后报错:Whitelabel Error Page (springboot关于addViewController无法映射页面问题)

具体的写完WebMvcConfigurerAdapter 的实现类如下:

package com.atguigu.springboot.config;

import com.atguigu.springboot.component.MyLocaleResolver;

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
//@EnableWebMvc   不要接管SpringMVC
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {



    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // super.addViewControllers(registry);
        //浏览器发送 /atguigu 请求来到 success
        registry.addViewController("/atguigu").setViewName("success");
    }

    //所有的WebMvcConfigurerAdapter组件都会一起起作用
    @Bean //将组件注册在容器
    public WebMvcConfigurerAdapter webMvcConfigurerAdapter(){
        WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("index");
                registry.addViewController("/index.html").setViewName("index");
                registry.addViewController("/main.html").setViewName("dashboard");
            }

            //注册拦截器
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                //super.addInterceptors(registry);
                //静态资源;  *.css , *.js
                //SpringBoot已经做好了静态资源映射
//                registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
//                        .excludePathPatterns("/index.html","/","/user/login");
            }
        };
        return adapter;
    }

    @Bean
    public LocaleResolver localeResolver(){

        return new MyLocaleResolver();
    }

}

  这里面刚刚开始配置的当访问的时候  就报如下的错误:

 这是因为

最终原因是thymeleaf的版本和springboot版本有冲突

后来最终改成的是:

<properties>
        <java.version>1.8</java.version>
        <!--下面两局指定thymeleaf的版本-->
        <!--布局功能的支持程序thymeleaf3主程序layout2以上版本
        thymeLeaf2版本主程序和Layout1适配

        Layout  是用来做布局功能扩展的
-->
        <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>   ---这 是改为正确的后,也就是换了下dialect标签中的类型。
        <thymeleaf-layout-dialect.version>2.3.0</thymeleaf-layout-dialect.version>


     <!--    <property> <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>    ----这是最开始的dialect的版本,
 <thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version> </property>--> </properties>

  

您的资助是我最大的动力!
金额随意,欢迎来赏!

如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的推荐按钮。
如果,您希望更容易地发现我的新博客,可以关注我哦!关注我

如果,想给予我更多的鼓励,这可以支付宝扫码哦!求打

因为,我的写作热情也离不开您的肯定支持,感谢您的阅读,我是【未来可期】!

原文地址:https://www.cnblogs.com/isme-zjh/p/14830508.html