【踩坑-SpringBoot】Thymeleaf+SpringBoot项目本地正常运行,部署服务器时Thymeleaf资源不存在报错

Thymeleaf+SpringBoot项目本地正常运行,部署服务器时Thymeleaf资源不存在报错


防爬虫:该文章原作者->迷恋

一、总结

先说本帖重点:如果是报错类似某个thymeleaf模板不存在的情况,可能是因为环境问题,导致其路径的错误。

  1. thymeleaf引用方面:th:replace="..."中引用的其他模板前面如果加了/,去掉。(th:replace="/.../..."改成th:replace=".../..."
  2. Controller方面:return的视图路径前面如果加了/,去掉。(return "/.../...";改成return ".../...";)

二、参考

如果没解决,可以参考其他文章或者继续百度(百度简直就是屎堆里找黄金,建议关注我,嘿嘿,好东西我都会写出来):
SpringBoot项目在本地正常运行,但是部署到服务器之后报错,报错信息是找不到模板
springboot使用Thymeleaf报错常见的几种解决方案
解决Springboot项目打包后的页面丢失问题(thymeleaf报错)


三、经过

写了一个Thymeleaf+SpringBoot的项目,想部署到服务器上,结果一访问主页就开始报错了,报错信息系大致如下:

在这里插入图片描述
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/user/index.html]")

就是说我的主页thymeleaf模板(index.html)出现了一个错误,然后接着看下面的信息:

在这里插入图片描述
Caused by: org.thymeleaf.exceptions.TemplateInputException: Error resolving template [/user/common_head.html], template might not exist or might not be accessible by any of the configured Template Resolvers (template: "user/index" - line 7, col 8)

因为我的user/index.html中引用了另一个模板user/common_head,如图:
在这里插入图片描述

报错信息的大致意思就是没有找到/user/common_head.html这个模板,所以其实问题就是出现在了这里。
通过百度查了各类帖子并尝试过后,发现就是这个最前面的/的问题,正确的写法应该去掉,即th:replace="user/common_head.html..." (把所有模板中的写法全改了一遍,累死)


你以为事情就结束了?,重新打包运行后,又出现类似问题:

在这里插入图片描述org.thymeleaf.exceptions.TemplateInputException: Error resolving template [/user/communicationPlate], template might not exist or might not be accessible by any of the configured Template Resolvers

又提示找不到/user/communicationPlate这个模板了,可是这个模板并没有被引用,而是有控制器直接返回:

在这里插入图片描述
想必大概也能猜个八九不离十,于是,又把return "/user/communicationPlate";改成了return "user/communicationPlate";(又改了一堆类似的控制器,哭)
再次打包运行,结果,完美运行,Nice~

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值