我有 Spring Boot Controller ,它返回对象列表,我使用 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)
看起来它不能使用驼峰大小写字母。不知道如何解决。
提前致谢。
最佳答案
这是 getter 方法的错误,它没有内联变量名,更改 getter thymeleaf 后能够正确解析它,愚蠢的错误。
关于java - Thymeleaf 不适用于驼峰类字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60978751/