当前位置:网站首页>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})|">
只要方法的参数中用到了 ${} 语法,那么我们就必须要使用拼接字符 || 将我们的方法进行包裹。
边栏推荐
- 嵌入式系统驱动初级【4】——字符设备驱动基础下_并发控制
- 力扣:343. 整数拆分
- Typora 使用保姆级教程 | 看这一篇就够了 | 历史版本已被禁用
- npm安装依赖报错npm ERR! code ENOTFOUNDnpm ERR! syscall getaddrinfonpm ERR! errno ENOTFOUND
- CentOS7 - yum install mysql
- JNI基本使用
- Resolved error: npm WARN config global `--global`, `--local` are deprecated
- FFmpeg源码分析:avformat_open_input
- Cannot read properties of null (reading 'insertBefore')
- (Kettle) pdi-ce-8.2 连接MySQL8.x数据库时驱动问题之终极探讨及解决方法分析
猜你喜欢

7.18 Day23 - the markup language

C language -- operator details

解决安装nbextensions后使用Jupyter Notebook时出现template_paths相关错误的问题

Teenage Achievement Hackers Need These Skills

高性能高可靠性高扩展性分布式防火墙架构

Performance testing with Loadrunner

Sublime Text 3 2021.8.3 个人配置

MySQL log articles, binlog log of MySQL log, detailed explanation of binlog log
![Deploy LVS-DR cluster [experimental]](/img/ad/84e05a6421d668b0b6ba6eeba0c730.jpg)
Deploy LVS-DR cluster [experimental]

在被面试官说了无数次后,终于潜下心来整理了一下JVM的类加载器
随机推荐
warning C4251: “std::vector&lt;_Ty&gt;”需要有 dll 接口由 class“Test”的客户端使用错误
3面头条,花7天整理了面试题和学习笔记,已正式入职半个月
Typora 使用保姆级教程 | 看这一篇就够了 | 历史版本已被禁用
力扣:63. 不同路径 II
OpenRefine中的正则表达式
力扣:746. 使用最小花费爬楼梯
FFmpeg源码分析:avformat_open_input
代码重构:面向单元测试
解决安装nbextensions后使用Jupyter Notebook时出现template_paths相关错误的问题
npm报错Beginning October 4, 2021, all connections to the npm registry - including for package installa
How to view sql execution plan offline collection
ORACLE LINUX 6.5 安装重启后Kernel panic - not syncing : Fatal exception
Gartner 权威预测未来4年网络安全的8大发展趋势
如何将 DevSecOps 引入企业?
部署LVS-DR群集【实验】
SLSA 框架与软件供应链安全防护
8、自定义映射resultMap
MySQL database (basic)
C1认证之web基础知识及习题——我的学习笔记
PHP解决字符乱码问题(多种编码转换)