thymeleaf: th:value 在使用 th:field 时被忽略 [英] thymeleaf: th:value is ignored when using th:field

查看:87
本文介绍了thymeleaf: th:value 在使用 th:field 时被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got a form where I want to edit some user data. So the already stored data is put as th:value and after sending I validate with spring validation and want to give back the form on wrong input. I want the input field to have the value of the input by the user but it always gives me the stored input instead.

这就是输入字段的样子

<input type="text" th:value="${product.name}" th:field="*{name}" th:errorclass="fieldError"/>

如果表单是第一次加载,输入字段应该具有已存储数据的值.

If the form is loaded the first time the input fields should have the value of the already stored data.

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

If it's loaded after submit and with an validation error, the input fields should have the value of the user's input.

有没有办法做到这一点?

Is there a way to to that?

谢谢!

推荐答案

Attribute th:field 将替换属性 valueidname 在您的 input 标签中.

Attribute th:field will replace attributes value, id and name in your input tag.

相反,使用普通的 th:idth:valueth:name 而不使用 th:field>.然后你会得到你想要的.

Instead, use plain th:id, th:value and th:name without using th:field. Then you will get what you wanted.

然后它看起来像:

<input type="text" th:value="${product.name}" th:name="name" th:id="name" th:errorclass="fieldError"/>

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

这篇关于thymeleaf: th:value 在使用 th:field 时被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆