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

相关推荐
Java探秘者14 分钟前
Maven下载、安装与环境配置详解:从零开始搭建高效Java开发环境
java·开发语言·数据库·spring boot·spring cloud·maven·idea
攸攸太上15 分钟前
Spring Gateway学习
java·后端·学习·spring·微服务·gateway
苹果醋324 分钟前
大模型实战--FastChat一行代码实现部署和各个组件详解
java·运维·spring boot·mysql·nginx
罗曼蒂克在消亡31 分钟前
graphql--快速了解graphql特点
后端·graphql
潘多编程34 分钟前
Spring Boot与GraphQL:现代化API设计
spring boot·后端·graphql
大神薯条老师1 小时前
Python从入门到高手4.3节-掌握跳转控制语句
后端·爬虫·python·深度学习·机器学习·数据分析
hhzz1 小时前
Linux Shell编程快速入门以及案例(Linux一键批量启动、停止、重启Jar包Shell脚本)
android·linux·jar
2401_857622662 小时前
Spring Boot新闻推荐系统:性能优化策略
java·spring boot·后端
ZhongruiRao2 小时前
Springboot+PostgreSQL+MybatisPlus存储JSON或List、数组(Array)数据
spring boot·postgresql·json
IT学长编程2 小时前
计算机毕业设计 Java酷听音乐系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·音乐系统·计算机毕业设计选题