I am trying to get access to my thymeleaf objects list and iterate through it.
The item class I am attempting to access looks like this:
data class Orders(val orders: List<Order>)
data class Order(val orderNumber: Long,
val time: String,
val name: String,
val zipCode: Int,
val recipientType: String?,
val recipientName: String?,
val recipientZipCode: String?,
val recipientCity: String?,
val shoppingNote: String?,
val id: Int,
val orderRows: List<OrderRow>)
data class OrderRow(val id: Int,
val variantTitle: String,
val productTitle: String,
val amount: Int,
val units: List<Unit>)
data class Unit(val code: String,
val redeemed: Boolean?)
And the controller I am using to give the list of orders to my template is as follows:
@RequestMapping("/user")
fun welcome(model: Model, principal: Principal): String {
val user = userService.getUserInfo(principal.name)
val listOfOrders = supplierService.getOrders(email, apiKey).orders
model.addAttribute("user", user)
model.addAttribute("listOfOrders", listOfOrders)
return "user"
}
and my template that I am using looks like this
<div class="container">
<table class="table" th:object="${listOfOrders}">
<thead>
<th>#</th>
<th>Navn</th>
<th>ID</th>
</thead>
<tbody>
<th:block th:each="order: ${listOfOrders}">
<tr class="accordion-toggle"
data-toggle="collapse"
th:data-target="'#accordion_'+${order.id}">
<td th:text="${order.orderNumber}"></td>
<td th:text="${order.name}"></td>
<td th:text="${order.id}"></td>
</tr>
<tr>
<td></td>
<td colspan="6">
<div th:id="'accordion_'+${order.id}" class="collapse in">
<table class="table">
<thead>
<th>#</th>
<th>Navn</th>
<th>ID</th>
</thead>
<tbody>
<th:block th:each="row: ${order.orderRows}">
<tr class="accordion-toggle"
data-toggle="collapse"
th:data-target="'#accordion_'+${row.id}">
<td th:text="${row.amount}"></td>
<td th:text="${row.productTitle}"></td>
<td th:text="${row.id}"></td>
</tr>
<tr>
<td></td>
<td colspan="6">
<div th:id="'accordion_'+${row.id}" class="collapse in">
Nothing to show yet.
</div>
</td>
</tr>
</th:block>
</tbody>
</table>
</div>
</td>
</tr>
</th:block>
</tbody>
</table>
</div>
and the first block where I use the listOfOrders as the th:object works perfectly, but for the second iteration where I try to access the orderRows of the Order fails with the two following messages
org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "order.orderRows" (template: "user" - line 50, col 39)
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'orderRows' cannot be found on null
I am currently stuck with this, because I also need to iterate through the orderRows Units once I can iterate through the Orders orderRows.
And I have verified that the listOfOrders added to the model does indeed have both orderRows and orderRows.units.
Any suggestion as to what I am missing or suggestions to get this to work?