当前位置:网站首页>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 Visual Effect Graph入门与实践
- 入坑软件测试的经验与建议
- Teenage Achievement Hackers Need These Skills
- Programming hodgepodge (4)
- Embedded system driver primary [3] - _IO model in character device driver foundation
- 9. Dynamic SQL
- MySQL日期函数
- The idea setting recognizes the .sql file type and other file types
- 心余力绌:企业面临的软件供应链安全困境
- 【论文阅读笔记】无监督行人重识别中的采样策略
猜你喜欢

The symbol table
![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

C语言 -- 操作符详解

sql server如何得到本条记录与上一条记录的差异,即变动值

Grain Mall - Basics (Project Introduction & Project Construction)

【问题解决】同一机器上Flask部署TensorRT报错记录

Can‘t connect to MySQL server on ‘localhost3306‘ (10061) 简洁明了的解决方法

8、自定义映射resultMap

谷粒商城-基础篇(项目简介&项目搭建)

PHP实现异步执行程序
随机推荐
EventBus源码分析
TensorRTx-YOLOv5工程解读(一)
动态规划总括
7. Execution of special SQL
7.16 Day22---MYSQL(Dao模式封装JDBC)
解决安装nbextensions后使用Jupyter Notebook时出现template_paths相关错误的问题
static在不同位置定义变量居然还有不同的含义?
[原创]STL容器map和unordered_map性能,创建,插入,随机访问速度对比!
JS基础--强制类型转换(易错点,自用)
Swoole学习(二)
注意!软件供应链安全挑战持续升级
Summary of MySQL database interview questions (2022 latest version)
8.03 Day34---BaseMapper查询语句用法
自动化测试的成本高效果差,那么自动化测试的意义在哪呢?
Embedded system driver primary [4] - under the basis of character device driver _ concurrency control
力扣题解8/3
想好了吗?
PHP解决字符乱码问题(多种编码转换)
大龄程序员的心理建设
通过&修改数组中的值