【问题标题】:Thymeleaf Select Dropdown Not PopulatingThymeleaf 选择下拉列表未填充
【发布时间】:2020-09-01 03:28:05
【问题描述】:

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

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

HomeFormBean 对象具有以下属性

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

页面加载时model属性设置如下

//this.userPaymentLookupList has the drop down values.
HomeFormBean homeFormBean = new HomeFormBean();
homeFormBean.setPaymentServiceTypeList(this.userPaymentLookupList);
model.addAttribute("homeFormBean", homeFormBean);

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

【问题讨论】:

    标签: spring-boot thymeleaf


    【解决方案1】:

    你使用:

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

    我个人的偏好是更新控制器并从HomeFormBean 中删除private List&lt;Lookup&gt; paymentServiceTypeList 字段。

    【讨论】:

      猜你喜欢
      • 2014-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多