이 컨텍스트에서는 숫자 또는 부울을 반환하는 변수 식만 허용됩니다.
내 javascript 함수에 값을 전달하려고 하는데, 그 함수 호출은 부울 변수에 의존합니다.최근에 백리프 보안 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.
어떻게 해결할 수 있습니까?감사해요.
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 을 참조하십시오.
이 접근 방식을 사용하여 작동할 수 있었습니다.
<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>
예외에서 제안된 것과 같은 다른 접근법은 감사합니다.
이렇게 해보세요.
<body th:onload="${timerEnabled eq true} ? 'javascript:runTimer(\'' + ${timeRemaining} + '\');'">
작동하지 않는 경우 다음을 사용해 볼 수도 있습니다.th:if
.
<th:block th:if="${timerEnabled} eq true">
<body th:onload="javascript:runTimer(\'' + ${timeRemaining} + '\');'">
</body>
</th:block>
<th:block th:if="${timerEnabled} eq false">
<body></body>
</th:block>
맞아요, 다른 버전이 훨씬 좋아 보이는데, 작동이 안 돼서 이번 버전도 나쁘지 않네요.물론 이런 경우에는 당신의 거주지에 그것을 추가하는 것을 추천하지 않을 것입니다.
제가 이상하게 생각하는 것은, 당신의 코드를 시도해본 결과, 제 쪽에서 작동한다는 것입니다.당신이 왜 그 오류를 얻는지 누가 알겠어요.
언급URL : https://stackoverflow.com/questions/52933002/only-variable-expressions-returning-numbers-or-booleans-are-allowed-in-this-cont
'programing' 카테고리의 다른 글
SQL Server 2005 제약 조건이 있는 드롭 열 (0) | 2023.06.26 |
---|---|
데이터를 파일이 아닌 문자열로 CSV 형식으로 쓰는 방법은 무엇입니까? (0) | 2023.06.26 |
수출 신고형 Xyz'는 '수출 신고형 Xyz'에 비해 무엇을 의미합니까? (0) | 2023.06.26 |
R.exe, Rcmd.exe, Rscript.exe와 Rterm.exe: 차이점은 무엇입니까? (0) | 2023.06.26 |
방화벽에서 호스트된 사이트를 제거하려면 어떻게 해야 합니까? (0) | 2023.06.26 |