Error resolving template [], template might not exist or might not be accessible by any of the conf

本文介绍了SpringBoot项目在使用java-jar运行时出现的Error resolving template错误的解决方法。主要涉及检查并添加Thymeleaf依赖,配置application.yml以及确保返回ModelAndView对象。通过这些步骤,可以成功解决无法找到模板的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、问题:

  • 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~

三、总结

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值