【问题标题】:Error while parsing thymeleaf template for list of items, using Textual syntax template, Throws error when parsing the list使用文本语法模板为项目列表解析 thymeleaf 模板时出错,解析列表时抛出错误
【发布时间】:2023-02-22 23:59:38
【问题描述】:

这是我的模板,我想列出每个项目的项目 ID。但是不断出错

[# th:each="item : ${items}"]
  - [(${item.id})]
[/]

我正在使用 kotlin 代码

 val items = listOf(Item(id = "CocaCola123", qty = BigDecimal.ONE))
        val templateEngine = TemplateEngine()
        val ctx = Context()
        ctx.setVariable("item", items)
        templateEngine.process(template, ctx)

我收到如下所示的错误

[main] ERROR org.thymeleaf.TemplateEngine - [THYMELEAF][main] Exception processing template "[# th:each="item : ${items}"]   - [(${item.id})] [/]": An error happened during template parsing (template: "[# th:each="item : ${items}"]
  - [(${item.id})]
[/]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "[# th:each="item : ${items}"]
  - [(${item.id})]
[/]")

Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating OGNL expression: "item.id" (template: "[# th:each="item : ${items}"]
  - [(${item.id})]
[/]" - line 2, col 7)

帮助我做错了什么? 我正在关注 thymeleaf 网站处理文本语法的文档。

模板

[# th:each="item : ${items}"]
  - [(${item})]
[/]

让我输出

[# th:each="item : ${items}"]
  - [Item(id=CocaCola123, qty=1)]
[/]"

我无法访问该 ID

【问题讨论】:

    标签: spring-boot kotlin


    【解决方案1】:

    // Change line

    ctx.setVariable("项目", 项目)

    // to bellow line

    ctx.setVariable("物品", 物品)

    // observe the change in key which you are using

    【讨论】:

    • 这是我尝试的第一件事,我想我需要配置 textTemplateResolver 但这也没有帮助
    猜你喜欢
    • 1970-01-01
    • 2016-12-21
    • 2021-11-05
    • 2014-08-26
    • 1970-01-01
    • 2021-09-25
    • 2019-08-05
    • 2020-10-07
    • 1970-01-01
    相关资源
    最近更新 更多