无法使用Thymeleaf渲染对象:在"org.parse4j.ParseObject"类型的对象上找不到属性或字段-可能不是公共的? [英] Cannot render Object with Thymeleaf: Property or field cannot be found on object of type 'org.parse4j.ParseObject' - maybe not public?
问题描述
我一直在遵循本指南 http://www.thymeleaf.org/doc/articles/springmvcaccessdata.html 了解如何使用Thymeleaf将数据模型渲染到Springboot应用程序中.我有一个函数可以从我的Parse-Server检索对象列表并将其呈现为模型属性:
I've been following this guide http://www.thymeleaf.org/doc/articles/springmvcaccessdata.html to learn how to render data models into a Springboot application with Thymeleaf. I have a function that retrieves a list of objects from my Parse-Server and renders them as model attributes:
@RequestMapping(value = "/requests", method = RequestMethod.GET)
public String findRequestsByCurrentUser(Model model) {
ParseUser currentUser = ParseUser.getCurrentUser();
log.info(String.valueOf(currentUser.getObjectId()));
findRequestsByCurrentUser(model, currentUser);
return "requests";
}
private void findRequestsByCurrentUser(Model model, TmtUser currentUser) {
ParseQuery<ParseObject> requestsQuery = ParseQuery.getQuery(ParseConstantsUtil.CLASS_REQUEST);
requestsQuery.whereContains(ParseConstantsUtil.REQUEST_AUTHOR, currentUser.getObjectId());
try {
List<ParseObject> requestsArrayList = requestsQuery.find();
model.addAttribute("requests", requestsArrayList);
log.info(String.valueOf(requestsArrayList));
log.info(String.valueOf(requestsArrayList.size()));
} catch (ParseException e) {
e.printStackTrace();
}
}
这是我发送到视图的模型的调试:
Here is a debug of the model that I send to my view:
我能够渲染对象,因为我可以包括静态文本而不是其属性,并且它将循环15次(在查询中检索到的对象数).但是每当我输入request.requestText
或request.requestStatus
时,我都会得到一个错误:
I am able to render the objects because I can include static text instead of its attributes and it will loop 15 times (the number of objects retrieved in the query). But whenever I type request.requestText
or request.requestStatus
I get an error:
<center>
<table class="table table-striped">
<tr>
<td><b>Requested By</b>
</td>
<td><b>Reason</b>
</td>
<td><b>Requested Date</b>
</td>
<td><b>Status</b>
</td>
</tr>
<tr th:each="request : ${requests}">
<div th:switch="${request.requestStatus}">
<div th:case="Approved">
<td th:text="${request.author.objectId" class="initial-name">Employee Initials
</td>
<td th:text="${request.requestText}">Request Description</td>
<td th:text="${request.dateRequested}">Request Date</td>
<td th:switch="${request.requestStatus}">
<span class="red" th:case="Pending" th:text="Pending">Status</span>
<span class="green" th:case="Approved" th:text="Approved">Status</span>
<span class="red" th:case="Rejected" th:text="Rejected">Status</span>
</td>
</div>
</div>
</tr>
</table>
</center>
例外:
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'requestText' cannot be found on object of type 'org.parse4j.ParseObject' - maybe not public?
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:207) ~[spring-expression-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:96) ~[spring-expression-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]
at org.springframework.expression.spel.ast.PropertyOrFieldReference.access$000(PropertyOrFieldReference.java:48) ~[spring-expression-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]
at org.springframework.expression.spel.ast.PropertyOrFieldReference$AccessorLValue.getValue(PropertyOrFieldReference.java:358) ~[spring-expression-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]
at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:88) ~[spring-expression-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]
at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:120) ~[spring-expression-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:267) ~[spring-expression-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT]
at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:263) ~[thymeleaf-spring5-3.0.3.M1.jar:3.0.3.M1]
... 116 common frames omitted
为什么Thymeleaf无法访问ParseObject属性?
Why are the ParseObject attributes not accessible to Thymeleaf?
当我自己打印${request}
时,我得到:org.parse4j.ParseObject@5975192b
.我也可以检索${request.objectId}
.检索属性的语法是什么? ${request.data.requestText}
似乎也不起作用...
When I print ${request}
on its own I get: org.parse4j.ParseObject@5975192b
, for example. I can also retrieve ${request.objectId}
. What is the syntax for retrieving the attributes? ${request.data.requestText}
does not seem to work either...
推荐答案
当使用Hashmap访问模型属性时,它可以工作,而无需创建自定义ParseObject:
It works when access your model attributes using a Hashmap, bypassing the need to create a custom ParseObject:
<td th:text="${request.get('requestText')}">Request Description</td>
这篇关于无法使用Thymeleaf渲染对象:在"org.parse4j.ParseObject"类型的对象上找不到属性或字段-可能不是公共的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!