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

相关推荐
dreams_dream1 小时前
Django序列化器
后端·python·django
懷淰メ1 小时前
python3GUI--短视频社交软件 By:Django+PyQt5(前后端分离项目)
后端·python·django·音视频·pyqt·抖音·前后端
郝开1 小时前
Spring Boot 2.7.18(最终 2.x 系列版本)1 - 技术选型:连接池技术选型对比;接口文档技术选型对比
java·spring boot·spring
有意义2 小时前
从零搭建:json-server+Bootstrap+OpenAI 全栈 AI 小项目
前端·后端·llm
知兀2 小时前
【Spring/SpringBoot】SSM(Spring+Spring MVC+Mybatis)方案、各部分职责、与Springboot关系
java·spring boot·spring
汤姆yu3 小时前
基于springboot的民间救援队救助系统
java·spring boot·后端·救援队
IT_陈寒3 小时前
React性能优化实战:这5个Hooks技巧让我的应用快了40%
前端·人工智能·后端
ruleslol3 小时前
SpringBoot面试题04-BeanDefinition
spring boot
韩立学长3 小时前
基于Springboot的智慧管网灌溉系统i1agupa7(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·后端
一 乐3 小时前
高校教务|教务管理|基于springboot+vue的高校教务管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·教务管理