org.thymeleaf.exceptions.TemplateProcessingException:在此上下文中只允许返回数字或布尔值的变量表达式 [英] org.thymeleaf.exceptions.TemplateProcessingException: Only variable expressions returning numbers or booleans are allowed in this context

查看:21
本文介绍了org.thymeleaf.exceptions.TemplateProcessingException:在此上下文中只允许返回数字或布尔值的变量表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 thymeleaf th:onclick 属性来调用带有如下参数的 javascript 函数

I have been using thymeleaf th:onclick attribute to call javascript function with parameters as below

th:onclick="|myFunction('${parameter1}')|"

但随着 thymeleaf 3.1.10 这已被删除.他们建议使用 th:data 属性.

But with thymeleaf 3.1.10 this has been removed. and they are suggesting to use th:data attribute.

然而,我找到了如下解决方法,并且它们都运行良好.

I however found workaround on as below and both of them are working perfectly.

  1. th:attr="onclick=|myFunction('${parameter1}')|"
  2. th:onclick="@{myFunction('${parameter1}')}">

现在我不确定这些变通方法是否是正确的做事方式,如果是,哪种方法更好.

Now i am not sure if these workarounds are correct way to do things and if yes which one is the better way.

推荐答案

第一个会像你想要的那样工作——但是,你绕过了安全限制,现在你的页面容易受到 javascript 注入(这是最初的原因)已进行此更改).

The first will work like you want -- however, you are bypassing the the security restriction and now your pages are vulnerable to javascript injection (which is the original reason this change was made).

第二个只是简单的不起作用.它不会扩展变量 ${parameter1},而只是将其编码为这样的 url:

The second one just plain doesn't work. It doesn't expand out the variable ${parameter1}, instead just encoding it as a url like this:

onclick="myFunction?$%7Bparameter1%7D"

您确实应该按照页面上的说明进行操作.

You really should be doing it as shown on the page.

th:data-parameter1="${parameter1}" onclick="myFunction(this.getAttribute('data-parameter1'));"

这篇关于org.thymeleaf.exceptions.TemplateProcessingException:在此上下文中只允许返回数字或布尔值的变量表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆