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

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

问题描述

我有一个简单的文本输入字段,我必须从一个对象中设置默认值并将其最终值保存在其他对象中。以下代码无效。

 < div th:object =$ {form}> 
< input class =form-control
type =text
th:value =$ {client.name}//此行被忽略
th :字段= * {CLIENTNAME}/>
< / div>

表格是DTO对象,客户端



通过不工作我的意思是说 - 可以说初始值是client.name =Foo和form.clientName = null。
我需要输入字段显示值为Foo,并在表单提交后将form.clientName值变为Foo。但是输入字段不会显示任何内容,并且在提交时form.clientName值仍然为null;

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

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



您可以使用此方法。而不是使用 th:field 使用html id & 名称。使用 th设置值:value

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

希望这能帮助您


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天全站免登陆