解决Thymeleaf中onclick的坑
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>结果出现这样的问题:

这个问题很神奇,不影响正常运行,但这个红色波浪线让我非常难受
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Spring @Scheduler使用cron表达式时的执行问题详解
Spring给程序猿们带来了许多便利。下面这篇文章主要给大家介绍了关于Spring @Scheduler使用cron表达式时的执行问题的相关资料,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧2018-09-09
SpringBoot整合Mybatis与thymleft实现增删改查功能详解
MybatisPlus是国产的第三方插件, 它封装了许多常用的CURDapi,免去了我们写mapper.xml的重复劳动。本文将整合MybatisPlus实现增删改查功能,感兴趣的可以了解一下2022-12-12


最新评论