【问题标题】:ForEach thymeleaf javascriptForEach 百里香 javascript
【发布时间】:2018-04-27 00:17:01
【问题描述】:

我正在尝试在 javascript 中使用 thymeleaf。 当我将 th:each 与 html 一起使用时,我的代码工作正常

<table>
  <tr th:each="theme : ${les_themes_1}">
    <td th:text="${theme.id}">id</td>   
  </tr>
  </table>

但是当我在脚本中使用 forEach 时,它返回 null

<script   th:inline="javascript" >

/*<![CDATA[*/
 /*[# th:each="theme : ${les_themes_1}"]*/
        document.write(/*[[${theme}]]*/); //return null
        document.write(/*[[${theme.id}]]*/); //not working
   /*[/]*/
/*]]>*/

</script>

请帮助!谢谢!

【问题讨论】:

  • 您的示例适用于百里香叶 3...
  • 我用的是thymeleaf 2.1.3版本!我会尝试更改版本
  • 用 thymeleaf 3 工作正常,谢谢你的回答!

标签: javascript spring thymeleaf


【解决方案1】:

这类问题的历史相当悠久,可以追溯到 JSP 规范的早期。关于 JavaScript,需要记住的关键一件事是它根据服务器端 Java 组件生成的输出在浏览器中执行。这适用于 JSP 和 Javascript、Velocity 和 Javascript、JSTL 和其他自定义标签和 Javascript,以及 Thymeleaf 和 Javascript。在这种情况下,Thymeleaf 模板在 Javascript 之前执行。

了解这一点后,一个关键的故障排除策略就是简单地查看和检查您在运行时生成的 HTML 和 Javascript 源代码。

【讨论】:

    猜你喜欢
    • 2020-12-30
    • 2013-09-10
    • 1970-01-01
    • 2021-01-09
    • 2017-04-25
    • 2018-02-03
    • 2016-01-04
    • 2016-08-14
    • 2018-11-24
    相关资源
    最近更新 更多