thymeleaf在windows环境下引用模板正常,到linux环境报错的问题

Xy718 1,057 2019-11-12
2019-11-11 21:02:03.457 [https-jsse-nio-10008-exec-6] ERROR o.a.c.c.C.[.[.[/].[dispatcherServlet] - 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/page/join/applyInfo.html]")] with root cause
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [/tempL/_applyInfoUser], template might not exist or might not be accessible by any of the configured Template Resolvers (template: "page/join/applyInfo" - line 17, col 22)
	at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869)

以上报错信息是thymeleaf引用模板引擎因为找不到模板文件而报错
这是我的引用语句:


th:include=""属性用于thymeleaf引用模板后渲染
文件路径:classpath:/templates/page/join/appliInfo.html

然后是我的定义模板的文件:

th:fragment=""用于定义该部分的模板可以被引用
文件路径:classpath:/templates/page/tempL/_applyInfoUser.html

出问题的原因

从报出的异常上来看其实很简单,就是引用的模板找不到(不存在)
也就是我们引用的路径填写的不对
在windows环境下不论你引用路径前是否有/,thymeleaf都会在classpath:/templates/下查找你的文件
ps:我的thymeleaf配置:

然后将你的程序(项目)部署到Linux环境上时,如果你引用的模板文件前加上了/的话,thymeleaf会在系统路径下查找你的文件,这也就导致为什么部署到linux环境时引用模板会报错

解决

去掉/


冶心·练体·得技