最近学了springboot,springboot推荐使用thymeleaf模板,所以一直在用,然后发现许许多多的坑,这次就说说其中关于onclick的坑。
在一个button元素中,需要调用js的方法,并且传递两个来自model的参数,一开始我是这么写的
<button
th:onclick=" 'cookOver(' + ${item.getOid()} + ',' + ${item.getFid()} + ')' " >出餐
</button>
接下来加载html就报错了,报错信息如下:
org.thymeleaf.exceptions.TemplateProcessingException: Only variable expressions returning numbers or booleans are allowed in this context, any other datatypes are not trusted in the context of this expression, including Strings or any other object that could be rendered as a text literal. A typical case is HTML attributes for event handlers (e.g. "onload"), in which textual data from variables should better be output to "data-*" attributes and then read from the event handler. (template: "/manage/cookWindow" - line 32, col 13)
神奇的是·,同一个项目中的另一个HTML中,也是这样的写法,没有报错,成功运行,参数传递也都没有问题。
百思不得其解,于是到网上查了下,发现还有一种另写法,我就试了试
<button
th:onclick="cookOver([[${item.getOid()}]],[[${item.getFid()}]])">出餐
</button>
结果出现这样的问题:
这个问题很神奇,不影响正常运行,但这个红色波浪线让我非常难受
感谢这篇帖子thymeleaf 的 th:onclick,慢慢采坑_thymeleaf th:onclick-CSDN博客