一、问题:

  • Error resolving template [/index], template might not exist or might not be accessible by any of the configured Template Resolvers
  • 出现在 SpringBoot 项目使用 java -jar 运行时

二、解决

  • 分别检查下面三个地方

2.1 在 pom.xml 中添加 thymeleaf 依赖

  • 没有的话加下,有的话不用加
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2.2 application.yml添加相应的thymeleaf 的配置

  • 我原先的配置不全,是错误的
  • 需要修改成下面这样
thymeleaf:prefix: classpath:/templates/  #prefix:指定模板所在的目录check-template-location: true  #check-tempate-location: 检查模板路径是否存在cache: false  #cache: 是否缓存,开发模式下设置为false,避免改了模板还要重启服务器,线上设置为true,可以提高性能。suffix:  .html#encoding: UTF-8#content-type: text/htmlmode: HTML5
  • 示例图(方便大家对应着看)

2.3 访问路径 @controller 的类

  • 记住,页面需要以 ModelAndView 对象的形式放回
    @RequestMapping("/")public ModelAndView indexHtml(HashMap<String, Object> map) {ModelAndView mv =  new ModelAndView("index");return mv;}
  • 对于 index 的路径我是这样的,大家可以对照下

  • 到这里从新跑一下就能解决啦~ cheers~

三、总结