Thymeleaf+SpringBoot项目本地正常运行,部署服务器时Thymeleaf资源不存在报错
防爬虫:该文章原作者->迷恋
一、总结
先说本帖重点:如果是报错类似某个thymeleaf模板不存在的情况,可能是因为环境问题,导致其路径的错误。
- thymeleaf引用方面:
th:replace="..."
中引用的其他模板前面如果加了/
,去掉。(th:replace="/.../..."
改成th:replace=".../..."
) - 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~