关于Thymeleaf中onclick的坑

最近学了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博客

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值