一文讲清楚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+**工具集合总有一个你能用到的~

相关推荐
Q_Q5110082852 分钟前
python的婚纱影楼管理系统
开发语言·spring boot·python·django·flask·node.js·php
EyeDropLyq32 分钟前
线上事故处理记录
后端·架构
一线大码2 小时前
Gradle 高级篇之构建多模块项目的方法
spring boot·gradle·intellij idea
MarkGosling3 小时前
【开源项目】网络诊断告别命令行!NetSonar:开源多协议网络诊断利器
运维·后端·自动化运维
congvee3 小时前
springboot 学习第1期 - 创建工程
spring boot
Codebee3 小时前
OneCode3.0 VFS分布式文件管理API速查手册
后端·架构·开源
_新一3 小时前
Go 调度器(二):一个线程的执行流程
后端
estarlee3 小时前
腾讯云轻量服务器创建镜像免费API接口教程
后端
风流 少年4 小时前
Cursor创建Spring Boot项目
java·spring boot·后端
毕设源码_钟学姐4 小时前
计算机毕业设计springboot宿舍管理信息系统 基于Spring Boot的高校宿舍管理平台设计与实现 Spring Boot框架下的宿舍管理系统开发
spring boot·后端·课程设计