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

这个问题很神奇,不影响正常运行,但这个红色波浪线让我非常难受
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
简介Java的Hibernate框架中的Session和持久化类
这篇文章主要介绍了Java的Hibernate框架中的Session和持久化类,Hibernate是Java的SSH三大web开发框架之一,需要的朋友可以参考下2015-12-12
深入浅析drools中Fact的equality modes
这篇文章主要介绍了drools中Fact的equality modes的相关知识,本文通过图文实例代码相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2022-05-05
Java Swing组件下拉菜单控件JComboBox用法示例
这篇文章主要介绍了Java Swing组件下拉菜单控件JComboBox用法,结合具体实例形式分析了Swing组件下拉菜单控件JComboBox的具体定义、使用方法及相关使用注意事项,需要的朋友可以参考下2017-11-11


最新评论