一、问题:

  • 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/html
  mode: HTML5
  • 示例图(方便大家对应着看)

2.3 访问路径 @controller 的类

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

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

三、总结

  • 如果还有问题可以截图私聊我,我有时间的话可以帮大家看看
  • 如果本文对你有用,欢迎点赞活着关注支持,因为这是对我最大的鼓励 💗
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐