最近系统联调发现这么一个异常:
ERROR org.thymeleaf.TemplateEngine - [THYMELEAF][http-nio-8191-exec-2] Exception proc
essing template "OAEmbeddedHtml": An error happened during template parsing (template: "class path resource [templates/OAEmbedde
dHtml.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [temp
lates/OAEmbeddedHtml.html]")
乍一看,有点慌。网上查了资料,感觉被他们误导了,还以为是程序运行时没有找到相应的html资源模板,然后各种折腾。。。
后来仔细看异常信息,又发现这么一段异常信息:
Caused by: org.attoparser.ParseException: Cannot execute subtraction: operands are "null" and "null" (template: "OAEmbeddedHtml" - line 71, col 13)
at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393)
at org.attoparser.MarkupParser.parse(MarkupParser.java:257)
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230)
... 92 common frames omitted
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Cannot execute subtraction: operands are "null" and "null" (template: "OAEmbeddedHtml" - line 71, col 13)
at org.thymeleaf.standard.expression.SubtractionExpression.executeSubtraction(SubtractionExpression.java:99)
at org.thymeleaf.standard.expression.ComplexExpression.executeComplex(ComplexExpression.java:65)
异常很明显了,是htymeleaf解析html模板时出错了,可能是语法错误,也可能是其它错误。然后仔细检查了html模板,发现确实是语法可能有问题,下面的'-'应该用单引号引起来,否则成了加减号了!但是奇怪的是,本地运行时没问题的,部署到服务器就报错了。
<td th:text="${detail.historyMinCost}-${detail.historyMaxCost}"></td>
然后部署,测试,还是有问题。最终经过仔细查看异常信息,发现
Caused by: java.lang.ArithmeticException: / by zero
at java.math.BigDecimal.divideAndRound(BigDecimal.java:4137)
at java.math.BigDecimal.divide(BigDecimal.java:5214)
at java.math.BigDecimal.divide(BigDecimal.java:1564)
at java.math.BigDecimal.divide(BigDecimal.java:1594)
原来是除0错误啊!我只判断了为null确没有判断为0的情况
<td th:text="${detail.originalCost != null ? (#numbers.formatDecimal((detail.currentCost-detail.originalCost)*100.0/detail.originalCost,0,2)+'%') : '-'}"></td>
改成如下就可以了:
<td th:text="${detail.originalCost != null && detail.originalCost > 0 ? (#numbers.formatDecimal((detail.currentCost-detail.originalCost)*100.0/detail.originalCost,0,2)+'%') : '-'}"></td>
经验教训:这种异常一般都是html模板语法有错误,仔细看异常信息就可以了,一定要仔细看,看到最后!
网友评论