当前位置:网站首页>thymeleaf中onclick事件动态传递参数问题
thymeleaf中onclick事件动态传递参数问题
2022-08-04 05:25:00 【**往事随風**】
thymeleaf中onclick事件动态传递参数问题
1. 动态传递Number类型或Boolean类型
1.1 动态传递一个Number 类型
<button th:onclick="|test(${session.friend.id})|">测试</button>
1.2 动态传递一个Boolean类型
- 示例一:
<button th:onclick="|test(${session.userBasic.id == session.friend.id})|">测试一下</button>
- 示例二:
<button th:onclick="|test(${true})|">测试一下</button>
2. 动态传递字符串类型
如果传递的类型是一个字符串或者对象类型的数据则会报以下错误:
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: "index" - line 26, col 82)
原因是,我们在动态传递参数时用到了 ${} 语法,而这个语法不支持传递字符串或对象类型的数据,此时我们可以通过以下方式动态传递一个字符串:
备注:th:data中的data只是一个变量名,可以修改为其他的具有语义化的名称
<button th:data="${session.userBasic.nickName}" th:onclick="|out(this.getAttribute('data'))|">测试</button>
3. 动态传递多个参数
- 参数中不包含字符串(多个参数之间使用
,隔开)
<button th:onclick="|test(${session.user.id},${session.user.age})|">
- 参数中包含字符串(多个参数之间使用
,隔开)
<button th:data="${session.user.nickName}" th:onclick="|test(${session.user.id},${session.user.age},this.getAttribute('data'))|">
4. 说明
- 如果只是传递一个
固定的参数,并且没有用到${}语法的话,则不需要使用拼接字符||,如下所示的格式
<button th:onclick="test(123456)">
<button th:onclick="test('字符串')">
- 如果需要动态传递参数的话,则必须使用
${}语法,那么我们的方法就必须要使用拼接字符||进行包裹,如下所示的格式
<button th:onclick="|test(${session.user.id})|">
只要方法的参数中用到了 ${} 语法,那么我们就必须要使用拼接字符 || 将我们的方法进行包裹。
边栏推荐
- Unity开发类似Profile那样的数据分析工具
- The Road to Ad Monetization for Uni-app Mini Program Apps: Full Screen Video Ads
- Unity动画生成工具
- MySQL date functions
- 音视频相关基础知识与FFmpeg介绍
- 4.1 声明式事务之JdbcTemplate
- Teenage Achievement Hackers Need These Skills
- DP4398:国产兼容替代CS4398立体声24位/192kHz音频解码芯片
- Unity Visual Effect Graph入门与实践
- 7.15 Day21---MySQL----Index
猜你喜欢

8、自定义映射resultMap
![Embedded system driver primary [4] - under the basis of character device driver _ concurrency control](/img/96/5224d2de152eb738703cd201fb8407.png)
Embedded system driver primary [4] - under the basis of character device driver _ concurrency control

webrtc中的任务队列TaskQueue

What is the salary of a software testing student?

7.16 Day22---MYSQL(Dao模式封装JDBC)

3面头条,花7天整理了面试题和学习笔记,已正式入职半个月

word 公式编辑器 键入技巧 | 写数学作业必备速查表

4.3 Annotation-based declarative transactions and XML-based declarative transactions

MySQL数据库(基础)

SLSA 框架与软件供应链安全防护
随机推荐
Delphi-C端有趣的菜单操作界面设计
将自定义类型作为关联容器的key
【论文阅读笔记】无监督行人重识别中的采样策略
MediaCodec支持的类型
7.18 Day23 - the markup language
(Kettle) pdi-ce-8.2 连接MySQL8.x数据库时驱动问题之终极探讨及解决方法分析
文献管理工具 | Zotero
梳理CamStyle、PTGAN、SPGAN、StarGAN
Dynamic programming of the division of numbers
【问题解决】同一机器上Flask部署TensorRT报错记录
PHP实现异步执行程序
Web Basics and Exercises for C1 Certification - My Study Notes
即时通讯网 即时通讯音视频开发
在被面试官说了无数次后,终于潜下心来整理了一下JVM的类加载器
OpenGLES 学习之帧缓存
嵌入式系统驱动初级【3】——字符设备驱动基础中_IO模型
Summary of MySQL database interview questions (2022 latest version)
Cannot read properties of null (reading 'insertBefore')
谷粒商城-基础篇(项目简介&项目搭建)
Gartner 权威预测未来4年网络安全的8大发展趋势