首页 > 解决方案 > Thymeleaf:一个简单的 for 循环中的“异常评估 OGNL 表达式”

问题描述

我有这个控制器:

it.render(
   "my-list.html",
   mapOf( 
      "votes-positive" to repo.votesOf("x"),
  )
)

//...
data class Vote(
   val name: String,
   val type: Type,
) {
   enum class Type { POS, NEG }
}

我的模板文件"my-list.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
    <div th:each="vote : ${votes-positive}">
        <span th:text="${vote.name}"></span>
    </div>
</body>

运行它时出现此错误,这让我发疯:

[qtp119358627-23] ERROR org.thymeleaf.TemplateEngine - [THYMELEAF][qtp119358627-23] Exception processing template "my-list.html": Exception evaluating OGNL expression: "vote.name" (template: "my-list.html" - line 21, col 15)
org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating OGNL expression: "vote.name" (template: "my-list.html" - line 21, col 15)

是否有 Thymleaf 调试模式?有没有办法打印模型树?我不知道为什么表达是错误的。

标签: kotlinthymeleafjavalin

解决方案


尝试更改votes-positive为不使用连字符的内容。这被解释为 OGNL 减法表达式中的减号。


推荐阅读