javascript - 在此上下文中只允许返回数字或 bool 值的变量表达式

标签 javascript html spring spring-boot thymeleaf

我正在尝试将一个值传递给我的 javascript 函数,但该函数调用取决于一个 bool 变量。在我最近升级到 thymeleaf security 5 之前,我的工作一直很好。

这是代码片段。

<body th:onload="${timerEnabled} ? 'javascript:runTimer(\'' + ${timeRemaining} + '\');'">

timerEnabled 必须为真才能完成函数调用,但 thymeleaf 现在抛出异常

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. 

我该如何解决这个问题?谢谢。

最佳答案

从 Thymeleaf 3.0.10 开始,他们修复了一个关于未转义代码的安全漏洞。

尝试

<body th:onload="[[${timerEnabled}]] ? 'javascript:runTimer(\'' + 
[[${timeRemaining}]] + '\');'">

或者推荐的方式:

<body th:data1="${timerEnabled}"
  th:data2="${timeRemaining}"
    th:onload="this.getAttribute('data1') ? javascript:runTimer(this.getAttribute('data2'));">

阅读更多:https://github.com/thymeleaf/thymeleaf/issues/707 和: http://forum.thymeleaf.org/Thymeleaf-3-0-10-JUST-PUBLISHED-tt4031348.html#a4031353

关于javascript - 在此上下文中只允许返回数字或 bool 值的变量表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52933002/

相关文章:

javascript - 简单的生成器似乎坏了

javascript - agGrid AngularJS 1.x 示例不起作用

html - 3 列流体 flexbox 和 rtl

javascript - 将meteor中的表数据导出到excel

java - 当根 URL 命中时,Wildfly 上的 Spring 不会渲染 index.html

java - 使用 restTemplate 发送带有身份验证 header 的 GET 请求

javascript - 根据第二个数组将数组划分为多个部分

javascript - React Redux 困惑

html - 左边的边框总是填充容器中的空白空间

java - Spring Webflux 中实体管理器的问题