我目前得到的是以下解决方案:
<form id="registration-form" method="post" th:object="${registrationDto}"
th:action="@{/registration}">
<input id="name" type="text" name="name" th:field="*{name}"/>
<input id="password" type="password" name="password" th:value="*{password}"/>
</form>
这似乎有效,我相信,当您省略通过
th:field
显式指定字段时,Thymeleaf 将省略额外的检查。只要 name
属性与表单的 dto 属性正确匹配^^ Thymeleaf 仍然能够正确映射字段。
我不能说这是否是最好的解决方案,但至少它是可行的。