如何访问th:object列表并使用th:each遍历它?

时间:2018-09-18 11:47:33

标签: spring-boot kotlin thymeleaf

我正在尝试访问我的百里香对象列表并进行遍历。

我尝试访问的项目类如下:

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?)

我用来将订单列表提供给模板的控制器如下:

@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"
    }

和我正在使用的模板如下

<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>

并且我将listOfOrders用作th:object的第一个块运行良好,但是对于第二个迭代,我尝试访问Order的orderRows失败,并显示以下两条消息

  

org.thymeleaf.exceptions.TemplateProcessingException:异常   评估SpringEL表达式:“ order.orderRows”(模板:“用户”-   第50行,第39列)

     

org.springframework.expression.spel.SpelEvaluationException:EL1007E:   无法在null上找到属性或字段“ orderRows”

我目前对此深感困惑,因为一旦可以遍历Orders orderRows,我还需要遍历orderRows单位。

并且我已经验证了添加到模型中的listOfOrders确实确实具有orderRows和orderRows.units。

关于我所缺少的任何建议或使它起作用的建议吗?

0 个答案:

没有答案