Thymeleaf Select下拉列表未填充

时间:2020-08-30 18:44:09

标签: spring-boot thymeleaf

在我的Spring Boot应用程序中,我正在努力填充选择下拉列表。我要显示选择下拉代码的页面如下

searchbox = driver.find_element_by_id("search")

HomeFormBean对象具有以下属性

<form th:action="@{/payment}" modelAttribute="homeFormBean" th:object="${homeFormBean}" method="POST">
    <section id="cover" class="min-vh-100">
        <div id="cover-caption">
            <div class="container">
                <div class="row text-black">
                    <div class="col-xl-5 col-lg-6 col-md-12 col-sm-10 mx-auto text-center form p-4">
                        <div class="col-md-12 form-group">
                            <label>Type</label>
                            <select class="form-control" th:field="*{selectedPaymentType}">
                                <option value="">Choose...</option>
                                <option th:each="paymentServiceType : ${paymentServiceTypeList}"
                                        th:value="${paymentServiceType.code}"
                                        th:utext="${paymentServiceType.description}"/>
                            </select>
                        </div>
                        <div class="col-md-12">
                            <label>From Date</label>
                            <input type="datetime-local" class="form-control" th:name="fromDate">
                        </div>
                        <div class="col-md-12">
                            <label>To Date</label>
                            <input type="datetime-local" class="form-control" th:name="toDate">
                        </div>
                        <div class="form-group container">
                            <br/>
                            <button type="submit" class="btn btn-primary btn-lg">Go</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
</form>

页面加载后,模型属性设置如下

private List<Lookup> paymentServiceTypeList;
private String selectedPaymentType;
private String fromDate;
private String toDate;

完成所有这些操作后,我的select选项仍然没有填充值。我搞砸了什么?

1 个答案:

答案 0 :(得分:0)

您使用:

<option th:each="paymentServiceType : ${paymentServiceTypeList}"
                                        th:value="${paymentServiceType.code}"
                                        th:utext="${paymentServiceType.description}"/>

这意味着paymentServiceTypeList必须直接是模型的属性。

所以要么更新控制器以使用:

HomeFormBean homeFormBean = new HomeFormBean();
model.addAttribute("paymentServiceTypeList", this.userPaymentLookupList);
model.addAttribute("homeFormBean", homeFormBean);

或更新Thymeleaf模板以使用:

<option th:each="paymentServiceType : *{paymentServiceTypeList}"
                                        th:value="${paymentServiceType.code}"
                                        th:utext="${paymentServiceType.description}"/>

我个人的喜好是更新控制器并从private List<Lookup> paymentServiceTypeList中删除HomeFormBean字段。