报错org.thymeleaf.exceptions.TemplateInputException: Error resolving template解决方案
- 在controller层请求处理完了返回时,没有使用@RestController或@ResponseBody而返回了非json格式
解决方案:可以将@Controller换成@RestController,不过需要注意有没有其他的方法返回了html页面,会导致返回的不是页面而是字符串;最好的方法就是在你所请求的方法上面加一个@ResponseBody即可。 - 在你的controller层对应的方法返回html路径及名称时,在前面多加了一个/ 。
例如return “/index”,正式这个/导致报错的,解决:去掉返回前面的/即可,例如return “/index” 。 - 在使用springboot的过程中,如果使用thymeleaf作为模板文件,则要求HTML格式必须为严格的html5格式,必须有结束标签,否则会报错。( 该方法是最有可能的 )
解决办法如下:
在application.yml中添加以下配置
# 开启模板缓存(默认值: true )
spring.thymeleaf.cache=false
#Content-Type 的值(默认值: text/html )
spring.thymeleaf.content-type=text/html
# 要运⽤于模板之上的模板模式。另⻅ StandardTemplate-ModeHandlers( 默认值: HTML5)
spring.thymeleaf.mode=HTML5
再在pom.xml 添加以下依赖
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>