Html 如何设置 thymeleaf th:来自其他变量的字段值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25027801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set thymeleaf th:field value from other variable
提问by Tobou
I have a simple text input field where i have to set default value from one object and save its final value in other. The following code is not working.
我有一个简单的文本输入字段,我必须在其中设置一个对象的默认值并将其最终值保存在另一个对象中。以下代码不起作用。
<div th:object="${form}">
<input class="form-control"
type="text"
th:value="${client.name}" //this line is ignored
th:field="*{clientName}"/>
</div>
formis DTO object and clientis Entity object from database.
表单是 DTO 对象,客户端是数据库中的实体对象。
What is the correct way to solve this situation?
解决这种情况的正确方法是什么?
By not working I mean - lets say that initial values are client.name="Foo" and form.clientName=null. I need that input field display value is "Foo" and after form submission form.clientName value to become "Foo". But the input field displays nothing and on submission form.clientName value still is null;
不工作我的意思是 - 假设初始值是 client.name="Foo" 和 form.clientName=null。我需要输入字段显示值为“Foo”,并在表单提交后 form.clientName 值变为“Foo”。但是输入字段什么都不显示,提交时 form.clientName 值仍然为空;
If anyone is interested, solved this issue using following structure (found the answer in another question).
如果有人感兴趣,请使用以下结构解决此问题(在另一个问题中找到答案)。
th:attr="value = ${client.name}"
回答by Newbie
You could approach this method.
你可以接近这个方法。
Instead of using th:field
use html id
& name
. Set value using th:value
而不是使用th:field
使用 html id
& name
。使用设置值th:value
<input class="form-control"
type="text"
th:value="${client.name}" id="clientName" name="clientName" />
Hope this will help you
希望能帮到你
回答by Julien Feniou
If you don't have to come back on the page with keeping form's value, you can do that :
如果您不必返回页面并保留表单的值,您可以这样做:
<form method="post" th:action="@{''}" th:object="${form}">
<input class="form-control"
type="text"
th:field="${client.name}"/>
It's some kind of magic :
这是某种魔法:
- it will set the value = client.name
- it will send back the value in the form, as 'name' field. So you would have to change your form field, 'clientName' to 'name'
- 它将设置值 = client.name
- 它将以“名称”字段的形式发回表单中的值。因此,您必须将表单字段“clientName”更改为“name”
If you matter keeping you form's input values, like a back on the page with an user input mistake, then you will have to do that :
如果您保留表单的输入值很重要,例如用户输入错误返回页面,那么您将不得不这样做:
<form method="post" th:action="@{''}" th:object="${form}">
<input class="form-control"
type="text"
th:name="name"
th:value="${form.name != null} ? ${form.name} : ${client.name}"/>
That means :
这意味着:
- The form field name is 'name'
- The value is taken from the form if it exists, else from the client bean. Which matches the first arrival on the page with initial value, then the forms input values if there is an error.
- 表单字段名称是“名称”
- 如果存在,则从表单中获取该值,否则从客户端 bean 中获取。它将页面上的第一个到达与初始值匹配,然后在出现错误时表单输入值。
Without having to map your client bean to your form bean. And it works because once you submitted the form, the value arn't null but "" (empty)
无需将客户端 bean 映射到表单 bean。它有效,因为一旦您提交表单,该值就不是 null 而是 ""(空)
回答by Tamb
The correct approach is to use preprocessing
正确的做法是使用预处理
For example
例如
th:field="*{__${myVar}__}"
th:field="*{__${myVar}__}"
回答by Saulid Snake
So what you need to do is replace th:field with th:name and add th:value, th:value will have the value of the variable you're passing across.
所以你需要做的是用 th:name 替换 th:field 并添加 th:value,th:value 将具有你传递的变量的值。
<div class="col-auto">
<input type="text" th:value="${client.name}" th:name="clientName"
class="form control">
</div>
回答by Petr Kloza
It has 2 possible solutions:
它有两种可能的解决方案:
1) You can set it in the view by javascript... (not recomended)
1)您可以通过javascript在视图中设置它...(不推荐)
<input class="form-control"
type="text"
id="tbFormControll"
th:field="*{clientName}"/>
<script type="text/javascript">
document.getElementById("tbFormControll").value = "default";
</script>
2) Or the better solution is to set the value in the model, that you attach to the view in GET operation by a controller. You can also change the value in the controller, just make a Java object from $client.name and call setClientName.
2)或者更好的解决方案是在模型中设置值,您通过控制器在 GET 操作中附加到视图。您还可以更改控制器中的值,只需从 $client.name 创建一个 Java 对象并调用 setClientName。
public class FormControllModel {
...
private String clientName = "default";
public String getClientName () {
return clientName;
}
public void setClientName (String value) {
clientName = value;
}
...
}
I hope it helps.
我希望它有帮助。