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, ParseUser currentUser) {
ParseQuery<ParseObject> requestsQuery = ParseQuery.getQuery("Request");
requestsQuery.whereContains("author", currentUser.getObjectId());
requestsQuery.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> requestList, ParseException e) {
if (e == null) {
model.addAttribute("requests", requestList);
}
}
});
}
Here is a debug of the model that I send to my view:
For some reason I can render currentRole
just fine. But I can't render any of the individual attributes from the requests
part of the mode. Should I use request.data.requestStatus
or request.requestStatus
? Even ${request}
alone won't render the whole object. I've tried a few different ways. None seem to work. Here is my HTML:
<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.data.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>
In my HTML, I iterate over the request objects provided in requestList and then retrieve their attributes.
Is Thymeleaf sensitive in such a way that if I have one typo anywhere in my HTML none of the objects will render? What else could be going wrong? Do I need to cast my ParseObject to a Java Object? Do I need to pass an ArrayList as opposed to a List?
The problem seems to be in the rendering of the list itself. I removed all the attributes from the HTML and just provided static text for the list. It should have rendered 15 rows of static text but it simply doesn't render anything... I wonder what it could be.
I think you could access to your data without the need to create a pojo. You need to access it as a Hashmap
<td th:text="${request.data.get('requestText')}">Request Description</td>