【问题标题】:Model attribute value is not passed to input type text in Thymeleaf模型属性值未传递给 Thymeleaf 中的输入类型文本
【发布时间】:2021-03-05 20:49:56
【问题描述】:

我正在尝试将模型属性(entryNumber)值设置为 Thymeleaf 中的输入类型文本。但输入值始终为空,尽管存在 entryNumber 值。

Empty Entry Number field

请查看我的 AllocationControllerLedgerDtoreceive-allocation.html 文件。

分配控制器

/**
 * This method allows to enter receive allocation details.
 */
@GetMapping("/admin/receive-allocation")
public String receiveAllocation(final ModelMap model) {
    model.addAttribute("entryNumber", allocationService.getLedgerEntryNumber());
    model.addAttribute("votes", allocationService.getAllVotes());
    model.addAttribute("divisions", userService.getAllDivisions());
    return "receive-allocation";
}

当我调试应用程序时,ModelMap 中存在 entryNumber。参考下图。

entryNumber is present in the ModelMap

LedgerDto

public class LedgerDto {

@NotEmpty
private String voteNumber;
@NotEmpty
private String division;
@NotEmpty
private String entryNumber;

public String getVoteNumber() {
    return voteNumber;
}

public void setVoteNumber(String voteNumber) {
    this.voteNumber = voteNumber;
}

public String getDivision() {
    return division;
}

public void setDivision(String division) {
    this.division = division;
}

public String getEntryNumber() {
    return entryNumber;
}

public void setEntryNumber(String entryNumber) {
    this.entryNumber = entryNumber;
}

receive-allocation.html

<form th:action="@{/admin/receive-allocation}"
                                    th:object="${ledgerDto}" method="post">
                                    <div class="form-row">
                                        <div class="form-group col-md-2"
                                            th:classappend="${#fields.hasErrors('entryNumber')}? 'has-error':''">
                                            <label for="inputEntryNumber">Entry Number</label> <input
                                                type="text" class="form-control"
                                                th:field="*{entryNumber}">
                                            <p class="error-message"
                                                th:each="error: ${#fields.errors('entryNumber')}"
                                                th:text="${error}">Validation error</p>
                                        </div>
                                

请帮我解决这个问题。

【问题讨论】:

    标签: spring-boot thymeleaf


    【解决方案1】:

    当您想使用模型属性中的值预填充表单时,无法使用 th:field。在这种情况下,你有 th:with 组合 th:value

    <form name='form' action="#" th:action="@{/internal/user/account/datachange}" method='POST' th:object="${userDTO}">
            <table>
                <tr>
                    <td><label for="name">Name:</label></td>
                    <td><input type="text" name="name" id="name" th:with="name=*{name}" th:value="${user.name}" required></td>
                </tr>
            </table>
        </form>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-20
      • 2021-11-30
      • 1970-01-01
      • 2021-02-03
      • 2020-11-02
      • 2013-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多