【问题标题】:Thymeleaf: Only variable expressions returning numbers or booleans are allowed in this contextThymeleaf:在此上下文中只允许返回数字或布尔值的变量表达式
【发布时间】:2019-09-06 13:01:47
【问题描述】:

我有一个 SpringBoot 2.1.4.RELEASE RESTful Web Service 应用程序。使用 Spring Initializer、嵌入式 Tomcat、Thymeleaf 模板引擎,并打包为可执行 JAR 文件。

我的一个模板中有这段代码,

<tr th:each="menuPriceSummary: ${menus}" >  

...

    <a href="#" th:onclick="|changeAIColor('idAwesomeIconFAV${menuPriceSummary.menu.symbol}');| + 'performAjaxCall(\'' + @{/allmenupricesummary/switchfav/{id}(id=${menuPriceSummary.menu.symbol})} + '\');'" >              
        <span th:if="${menuPriceSummary.favorited}">
            <i th:id="'idAwesomeIconFAV'+${menuPriceSummary.menu.symbol}"  class="fa fa-toggle-on fa-lg" style="color:#009900; text-align: center;" aria-hidden="true"></i>
        </span>
        <span th:if="${!menuPriceSummary.favorited}">
            <i th:id="'idAwesomeIconFAV'+${menuPriceSummary.menu.symbol}" class="fa fa-toggle-off fa-lg"  style="color:#e6e6e6;" aria-hidden="true"></i>
        </span>
    </a>
</tr>

但是我在渲染模板时遇到了这个错误:

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.

【问题讨论】:

标签: html spring-boot spring-mvc thymeleaf


【解决方案1】:

您必须将 Thymeleaf 表达式移动到 th:data-* 属性,并改用 this.getAttribute('...')。例如这样的事情:

<tr th:each="menuPriceSummary: ${menus}" >  
    <a  href="#"
        th:data-icon="|idAwesomeIconFAV${menuPriceSummary.menu.symbol}|"
        th:data-url="@{/allmenupricesummary/switchfav/{id}(id=${menuPriceSummary.menu.symbol})}"
        onclick="changeAIColor(this.getAttribute('data-icon')); performAjaxCall(this.getAttribute('data-url'));">
        <span th:if="${menuPriceSummary.favorited}">
            <i th:id="'idAwesomeIconFAV'+${menuPriceSummary.menu.symbol}"  class="fa fa-toggle-on fa-lg" style="color:#009900; text-align: center;" aria-hidden="true"></i>
        </span>

        <span th:if="${!menuPriceSummary.favorited}">
            <i th:id="'idAwesomeIconFAV'+${menuPriceSummary.menu.symbol}" class="fa fa-toggle-off fa-lg"  style="color:#e6e6e6;" aria-hidden="true"></i>
        </span>
    </a>
</tr>

【讨论】:

  • 我知道这个解决方案,但是在这种情况下“这个”(它的类型)是什么意思?因为 intellij IDEA 给了我“无法解决”,我想为它添加 thymesVar id/type。
  • 您是否使用th:onclick 而不是onclickthis 指的是 &lt;a /&gt; 标签的 javascript dom 元素(并且不应该与 Thymeleaf 有任何关系)。
  • 我同意。但我不知道为什么 intellij IDEA 显示错误提示并说“无法解决这个问题”。
【解决方案2】:

你可以像下面这样使用

<div th:each="file: ${files}">
   <input type="button"  th:data-parameter1="${file.id}"  th:data-parameter2="${file.name}  th:onclick="passValue(this.getAttribute('data-parameter1'),this.getAttribute('data-parameter2'))" />
</div>

在 JavaScript 用户中

function test(id,name){
        
    }

【讨论】:

    猜你喜欢
    • 2019-06-10
    • 2019-03-26
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    相关资源
    最近更新 更多