【问题标题】:Bind dropdown value to a bean in Thymeleaf将下拉值绑定到 Thymeleaf 中的 bean
【发布时间】:2018-09-21 15:50:18
【问题描述】:

我有一个下拉框并尝试将所选帐号绑定到转移 bean 中的 fromAccount。

我在控制器中看到该值为 null。

            <select class="form-control" th:field="${customer.transferBean.fromAccount}">
            <option
                    th:each="fromAccount: ${customer.accountBean}"
                    th:value="${fromAccount.accountNum}"
                    th:text="${fromAccount.accountNum}" ></option>
            </select>

控制器代码:

@PostMapping("/accounttransfer")
public String accountTransfer(@Valid CustomerBean customerBean, BindingResult bindingResult, Model model)

不知道我错过了什么。我正在为 html 使用 spring boot 和 thymeleaf。

谢谢。

【问题讨论】:

    标签: spring spring-boot thymeleaf


    【解决方案1】:

    你使用了错误的语法

    "th:field="${customer.transferBean.fromAccount}"
    

    对于指定绑定字段,您应该使用*{} 而不是${},并且只能在父级或ansestor 级别上与th:object 结合使用。

    您可以在 Thymeleaf 文档中找到有关 bindingm 的更多详细信息

    【讨论】:

    • 谢谢。你是对的,我使用了错误的语法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-26
    • 2019-10-26
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多