如何设置 thymeleaf th:来自其他变量的字段值 [英] How to set thymeleaf th:field value from other variable

查看:44
本文介绍了如何设置 thymeleaf th:来自其他变量的字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的文本输入字段,我必须在其中设置一个对象的默认值并将其最终值保存在另一个对象中.以下代码不起作用.

<input class="form-control"类型=文本"th:value="${client.name}"//这行被忽略th:field="*{clientName}"/>

form 是 DTO 对象,client 是数据库中的实体对象.

解决这种情况的正确方法是什么?

我的意思是不工作 - 假设初始值为 client.name="Foo" 和 form.clientName=null.我需要输入字段显示值为Foo",并在表单提交后 form.clientName 值变为Foo".但是输入框什么都不显示,提交时form.clientName 的值仍然为空;

如果有人感兴趣,请使用以下结构解决此问题(在另一个问题中找到答案).

th:attr="value = ${client.name}"

解决方案

你可以使用这个方法.

不要使用 th:field 使用 html id &<代码>名称.使用 th:value

设置值

希望对你有帮助

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>

form is DTO object and client is Entity object from database.

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;

If anyone is interested, solved this issue using following structure (found the answer in another question).

th:attr="value = ${client.name}"

解决方案

You could approach this method.

Instead of using th:field use html id & name. Set value using th:value

<input class="form-control"
           type="text"
           th:value="${client.name}" id="clientName" name="clientName" />

Hope this will help you

这篇关于如何设置 thymeleaf th:来自其他变量的字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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