thymeleaf中使用th:onclick出现的问题

在SpringBoot 2.x与Thymeleaf结合的项目中遇到模板处理异常,当使用th:onclick属性时,直接插入变量会导致错误。错误信息提到必须将${region.enName}包裹在[[...]]内以避免TemplateProcessingException。然而,这样做虽然解决了编译问题,但onclick事件并未按预期执行。通过进一步研究,找到了使onclick事件正常工作的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在springboot2.x的环境中使用thymeleaf的onclick时,若安照如下方式使用onclick,就会报错TemplateProcessingException异常。

 <tr th:each="region, regionStat: ${regions}">
     <a th:attr="data-bind=${region.enName}" href="javascript:void(0);"
     th:onclick="'changeSimpleCondition(\'regionEnName\', \'' + ${region.enName} + '\')'"
     th:text="${region.cnName}"></a>
 </tr>

报错如下:

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: "rent-list" -
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值