Springboot报错:Could not resolve view with name 'index' in servlet with name 'dispatcherServlet'

发布时间 2023-04-11 11:39:48作者: super超人

该异常是因为用定义了带@EnableWebMvc注解的配置类后发生的,在带该注解的配置类中加入下面的代码就可以了:

@Bean
public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setPrefix("/WEB-INF/jsp/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
}

有了这段代码在application.yml中的下面的配置就没有必要了

spring: 
  mvc:
    view:
      prefix: /WEB-INF/
      suffix: .jsp

使用 @EnableWebMvc 注解,需要以编程的方式指定视图文件相关配置;
使用 @EnableAutoConfiguration 注解,会读取 application.properties 或 application.yml 文件中的配置。 

参考:

https://blog.csdn.net/father_Blogger/article/details/89352479

https://www.jianshu.com/p/403fcb340257