一文讲清楚SpringBoot项目打包jar后运行报错template might not exist - 第514篇

历史文章(文章累计500+)

国内最全的Spring Boot系列之一

国内最全的Spring Boot系列之二

国内最全的Spring Boot系列之三

国内最全的Spring Boot系列之四

国内最全的Spring Boot系列之五》

国内最全的Spring Boot系列之六

国内最全的Spring Boot系列之七》

ES 深度分页问题及针对不同需求下的解决方案ES系列 - 第509篇

抖音主播/电商人员有福了,利用Suno创作产品宣传,让产品动起来-小米Su7 - 第510篇

Spring Boot整合ElasticSearch实战 - 第511篇

Transaction rolled back because it has been marked as - 第512篇

五音不全也浪漫,521清华学霸为爱人写歌 - 第513篇

悟纤:脑瓜疼,脑瓜疼~

悟纤:此时此刻,我想XX,但我从小接受的教育不支持我做这种事情。

师傅:徒儿,何事如此暴躁~

悟纤:你说奇不奇怪,本地环境运行的好好,到线上就不正常了~

师傅:这种情况也是最头疼的,但也是最能体现对技术深度的了解~

悟纤:那师傅赶紧帮我一起瞅瞅这是怎么回事了。

师傅:Let me see~

导读

最近在开发一个AI导航项目(地址在下面)的时候,遇到了springboot打包之后运行报错 Error resolving template /base/meta, template not exist or might not be accessible这个异常,这个问题就很诡异,本地开发环境运行的就很正常,到线上就芭比Q了。

项目地址:http://ai.dzwlai.com/

AI导航站,汇总**800+**工具集合:

出现情况

idea直接运行一切正常,mvn package命令打包后访问/index时报错,报错信息如上下:

复制代码
ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - [log,175] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/NavWebsite/ai/site/site.html]")] with root causeorg.thymeleaf.exceptions.TemplateInputException: Error resolving template [/NavWebsite/ai/base/meta], template might not exist or might not be accessible by any of the configured Template Resolvers (template: "NavWebsite/ai/site/site.html" - line 4, col 11)        at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869)        at org.thymeleaf.engine.TemplateManager.parseStandalone(TemplateManager.java:250)​

解决方法

情况1:路径地址多了一个/

在你的controller层对应的方法返回html路径及名称时,在前面多加了一个/。例如return "/index",正式这个/导致报错的,解决:去掉返回前面的/即可,例如return "index" 。

情况2:未遵循Html5格式

在使用springboot的过程中,如果使用thymeleaf作为模板文件,则要求HTML格式必须为严格的html5格式,必须有结束标签,否则会报错。

解决方案:在application配置文件中配置

复制代码
#thymeleafspring.thymeleaf.prefix=classpath:/templates/spring.thymeleaf.suffix=.htmlspring.thymeleaf.mode=LEGACYHTML5spring.thymeleaf.encoding=UTF-8spring.thymeleaf.content-type=text/htmlspring.thymeleaf.cache=false​

情况3:资源文件的路径被修改

资源文件的路径被修改,如果你其他的请求都正常返回则可忽略这条。

解决:在pom.xml文件的中加入

复制代码
<resource>    <directory>src/main/resources </directory></resource>​

情况4:使用th:replace

在使用th:replace或者th:include的时候,多了一个/:

<th:block th:include="/base/meta.html"/>

以上这个代码在开发工具中可以正常访问到的,但到线上就不好说了。

解决方案:去掉路径最前方的"/"

<th:block th:include="base/meta.html"/>

情况5:依赖未添加

spring-boot-starter-thymeleaf未添加,个人觉得这种情况在开发工具中就应该发现了,不应该到线上环境,除非你写代码不测试。

以前有遇到一个同事,写一天代码才测试的,我只能用崇拜的小眼睛看他,我表示我是做不到。

我的问题

最后排查后发现出现的是第1种情况,放回的html路径前多了"/",导致打包后找不到html了,但是打包前可以正常访问。

其次就是第4中情况,在使用th:replace的时候,多了一个"/",导致部署到线上无法找到模板文件。

👉 🏻 👉 🏻 👉 🏻 最后大家可以收藏一下网址:

http://ai.dzwlai.com/

**800+**工具集合总有一个你能用到的~

相关推荐
ikoala几秒前
Codex 怎么买、怎么充值?先把这两套计费搞清楚
前端·javascript·后端
前端Hardy31 分钟前
一个时代结束了:npm 终于对 install 脚本下手了
前端·javascript·后端
damaoyou32 分钟前
Cog3DRangeImagePlaneEstimatorTool完全指南
后端
Nturmoils1 小时前
分页别写太顺手,LIMIT 背后还有排序和边界
数据库·后端
神奇小汤圆1 小时前
国产版“Codex”初体验,智谱ZCode很强啊!
后端
站大爷IP1 小时前
Python里的“赋值”到底是什么意思?
后端
鹅城剑仙2 小时前
Spring Boot 微服务架构设计与最佳实践
spring boot·后端·微服务
心之伊始2 小时前
Spring Boot Actuator + Micrometer 实战:自定义业务指标并接入 Prometheus 观测接口耗时
java·spring boot·prometheus·actuator·micrometer
Full Stack Developme2 小时前
Spring Integration 教程
java·后端·spring
爱勇宝3 小时前
AI 时代,前端工程师的话语权正在下降?
前端·后端