thymeleaf頁面調用js方法傳參報錯:exceptions.TemplateInputException:Only variable expressions returning 。。。

使用thymeleaf,html頁面調用js方法時候傳一個參數, 如下寫法不報錯:

<a th:onclick="'update('+ ${data.id} +');'"> 修改</a>

 但是,當傳入兩個參數的時候,代碼如下:

<a th:onclick="'update('+${data.id}+','+${data.name}+');'" >修改</a>

就報如下錯誤:

org.thymeleaf.exceptions.TemplateInputException: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. (template: "productList" - line 44, col 47)

 這就很奇怪,爲什麼一個傳參就可以,兩個不行,這個我查了半天沒找到原因,哪位大牛知道了,麻煩留言解惑一下。

那麼當兩個參數的時候,使用雙中括號就可以傳值了:

<a th:onclick="update([[${data.id}]],[[${data.name}]])" >修改</a>

這裏記錄一下,順便也給遇到同樣錯誤的小夥伴一個參考。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章