在这种情况下,只允许返回数字或布尔值的变量表达式 [英] Only variable expressions returning numbers or booleans are allowed in this context

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

问题描述

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

I am trying to pass a value to my javascript function but that function call depends on a boolean variable. I had this working fine until I recently upgraded to thymeleaf security 5.

这是代码段.

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

timerEnabled必须为true才能完成函数调用,但百里香现在会抛出一个异常

timerEnabled has to be true for the function call to be done but thymeleaf now throws an exception as

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. 

我该如何解决?谢谢.

推荐答案

通过使用这种方法,我能够使其正常工作

I was able to have it working by using this approach

<body>

<script th:inline="javascript">
    /*<![CDATA[*/

    var flag = [[${timerEnabled}]]; // if timer should be included or not
    var timeRemaining = [[${timeRemaining}]]; // the time remaining.
    window.onload = function() {
        if(!flag)
            return; // Exit/Return if the variable is false
        runTimer(timeRemaining); // Call your favourite method if the variable is true
    };

    /*]]>*/
</script>

赞赏其他任何方法,例如在异常中建议的方法.

Any other approach such as suggested in the exception is appreciated.

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

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