项目报错
问题
当我在前端页面调用后端查询数据时,因为前端使用了thymeleaf,报错如下:org.thymeleaf.exceptions.TemplateInputException: Error resolving template,因为花了挺长时间才解决,所以记录一下。
解决
1.、在controller层请求处理完了返回数据时,没有使用@RestController或@ResponseBody而返回了非json格式
这种情况下返回的数据thymeleaf模板无法解析,这也正是我报错的原因。
解决方式:最好的解决方式就是在你所请求的方法上面加一个@ResponseBody即可。或者将@Controller换成@RestController,不过需要注意有没有其他的方法返回了html页面。
另外,我在网上找到了一些其他原因导致这个错误的,这里也记录一下
2、在你的controller层对应的方法返回html路径及名称时,在前面多加了一个/
去掉/即可
3、在使用springboot的过程中,如果使用thymeleaf作为模板文件,则要求HTML格式必须为严格的html5格式,必须有结束标签,否则会报错。
解决方法:
添加配置文件:
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>
1.9.22
本文参考自:https://blog.youkuaiyun.com/Lin_xiaofeng/article/details/79122053 有更多的关于thymeleaf报错的解决方案