【问题标题】:Thymeleaf not working for camelcase class fieldsThymeleaf 不适用于驼峰类字段
【发布时间】:2020-04-04 01:07:42
【问题描述】:

我有返回对象列表的 Spring Boot 控制器,我正在使用 thymeleaf 显示 html 中的记录,代码如下:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>AllRecords</title>
</head>
<body>
    <h3>Showing all records</h3>
    <table border="1">
        <tr>
            <td>Title</td>
            <td>Publised At</td>
            <td>Author</td>

        </tr>
        <tr th:each="news : ${newsList}">
            <td th:text="${news?.title}">Title</td>
            <td th:text="${news.publishedAt}">Publised At</td>
            <td th:text="${news?.author}">Author</td>
        </tr>
    </table>
</body>
</html>

Thymeleaf 无法解析“publishedAt,显示如下错误

Caused by: org.thymeleaf.exceptions.TemplateProcessingException:
Exception evaluating SpringEL expression: "news.publishedAt" (template: "showNews" - line 19, col 8)
    at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:290)
    at org.thymeleaf.standard.expression.VariableExpression.executeVariableExpression(VariableExpression.java:166)
    at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:66)
    at org.thymeleaf.standard.expression.Expression.execute(Expression.java:109)

看起来它不适用于驼峰式字母。不知道怎么解决。

提前致谢。

【问题讨论】:

  • 发布整个错误以及您的news 对象定义,以便我们查看需要更改的内容。
  • 可能是拼写错误“publishedAt”而不是“publishedAt”

标签: java html spring spring-boot thymeleaf


【解决方案1】:

这是getter方法的错误,它没有与变量名内联,更改getter thymeleaf后能够正确解析它,愚蠢的错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 2015-09-27
    • 1970-01-01
    • 1970-01-01
    • 2015-02-01
    • 1970-01-01
    • 2019-07-07
    相关资源
    最近更新 更多