thymeleaf:th:使用th:field时忽略值

Rap*_*ael 9 thymeleaf

我有一个表单,我想编辑一些用户数据.因此,已经存储的数据被放置为:值并且在发送之后我使用spring验证进行验证并且想要在错误的输入上返回表单.我希望输入字段具有用户输入的值,但它总是给我存储的输入.

这就是输入字段的样子

<input type="text" th:value="${product.name}" th:field="*{name}" th:errorclass="fieldError"/>
Run Code Online (Sandbox Code Playgroud)

如果首次加载表单,则输入字段应具有已存储数据的值.

如果在提交后加载并且出现验证错误,则输入字段应具有用户输入的值.

有办法吗?

谢谢!

小智 18

属性日:现场将替换属性,标识名称在您输入的标签.

相反,使用plain th:id,th:valueth:name而不使用th:field.然后你会得到你想要的.

然后它看起来像:

<input type="text" th:value="${product.name}" th:name="name" th:id="name" th:errorclass="fieldError"/>
Run Code Online (Sandbox Code Playgroud)

类似的答案在这里:如何设置百里香通:来自其他变量的字段值