JavaScript関数に値を渡そうとしていますが、その関数呼び出しはブール変数に依存しています。最近thymeleaf security 5にアップグレードするまで、これは問題なく機能していました。
これはコードスニペットです。
<body th:onload="${timerEnabled} ? 'javascript:runTimer(\'' + ${timeRemaining} + '\');'">
timerEnabledは、関数呼び出しを実行するためにtrueである必要がありますが、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.
どうすればこれを解決できますか?ありがとうございました。
私はこのアプローチを使用してそれを機能させることができました
<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>
例外で提案されているような他のアプローチは高く評価されます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加