Springboot報錯:Could not resolve view with name 'index' in servlet with name 'dispatcherServlet'

該異常是因爲用定義了帶@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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章