最近在使用springboot+thymeleaf寫程序的時候,一直報錯org.thymeleaf.exceptions.TemplateInputException: Error resolving template硬是搞了整整一天才解決,因為自己踩過坑了,所以,在這里,本人給出目前我所搜集到的所有報這個錯的情況的解決方案。
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "xxx/xxx", template might not exist or might not be accessible by any of the configured Template Resolvers
at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:865) ~[thymeleaf-3.0.2.RELEASE.jar:3.0.2.RELEASE]
at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:608) ~[thymeleaf-3.0.2.RELEASE.jar:3.0.2.RELEASE]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1087) [thymeleaf-3.0.2.RELEASE.jar:3.0.2.RELEASE]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1061) [thymeleaf-3.0.2.RELEASE.jar:3.0.2.RELEASE]
-
在controller層請求處理完了返回時,沒有使用@RestController或@ResponseBody而返回了非json格式
這種情況下返回的數據thymeleaf模板無法解析,直接報錯,本人正式因為這個原因才報錯。解決方案:可以將@Controller換成@RestController,不過需要注意有沒有其他的方法返回了html頁面,會導致返回的不是頁面而是字符串;最好的方法就是在你所請求的方法上面加一個@ResponseBody即可。
-
在你的controller層對應的方法返回html路徑及名稱時,在前面多加了一個/ 。
例如return "/index",正式這個/導致報錯的,解決:去掉返回前面的/即可,例如return "/index" 。
-
在使用springboot的過程中,如果使用thymeleaf作為模板文件,則要求HTML格式必須為嚴格的html5格式,必須有結束標簽,否則會報錯。
解決辦法如下:
在application.yml中添加以下配置
spring.thymeleaf.content-type: text/html
spring.thymeleaf.cache: false
spring.thymeleaf.mode: LEGACYHTML5
再在pom.xml 添加以下依賴
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
-
資源文件的路徑被修改,如果你其他的請求都正常返回則可忽略這條。
解決:在pom.xml文件的
中加入
<resource>
<directory>src/main/resources</directory>
</resource>
目前本人就遇到上面四種報錯原因,希望可以幫助到你。
————————————————
版權聲明:本文為CSDN博主「林曉風」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/Lin_xiaofeng/article/details/79122053