如何从其他变量设置 thymeleaf th:field 值

问题描述 投票:0回答:7

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

<div th:object="${form}">
    <input class="form-control"
           type="text"
           th:value="${client.name}"  //this line is ignored
           th:field="*{clientName}"/>
</div>

form是DTO对象,client是来自数据库的实体对象。

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

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

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

th:attr="value = ${client.name}"
html spring spring-mvc thymeleaf
7个回答
63
投票

你可以采用这个方法。

不要使用

th:field
,而是使用 html
id
name
。使用
th:value

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

希望这对你有帮助


11
投票

正确的做法是使用预处理

例如

th:field="*{__${myVar}__}"


2
投票

如果您不必返回页面并保留表单的值,您可以这样做:

<form method="post" th:action="@{''}" th:object="${form}">
    <input class="form-control"
           type="text"
           th:field="${client.name}"/>

这是某种魔法:

  • 它将设置值 = client.name
  • 它将发送回表单中的值,作为“名称”字段。因此,您必须将表单字段“clientName”更改为“name”

如果您关心保留表单的输入值,例如页面上出现用户输入错误的背面,那么您必须这样做:

<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}"/>

这意味着:

  • 表单字段名称为“name”
  • 如果表单存在,则从表单中获取该值,否则从客户端 bean 获取。它将首先到达的页面与初始值相匹配,如果有错误,则表单输入值。

无需将客户端 bean 映射到表单 bean。 它之所以有效,是因为一旦您提交了表单,该值就不是 null 而是“”(空)


2
投票

好吧,如果我正确理解了这个问题,那么您正在尝试为变量分配一个预定值,然后在 Thymeleaf 渲染该变量时将该变量填充到 html 表单中。如果这就是您所问的问题,我也已经为此苦苦挣扎了几天,在阅读您的帖子后才找到答案,并认为我会分享。

我正在做的事情(希望这对某人有帮助)是尝试自动为我的博客文章的 id 分配一个值,以便当用户提交它时,博客文章会自动获取我想要分配的 ID。

*** 我遇到的问题*** 当我像这样分配 id 变量 { th:field="*{id}" } 时,表单输入框中的输出始终为“0”。所以我尝试了各种不同的方法来解决这个问题,比如添加一个 th:value 和一个 th:name,这总是导致大的“0”。

所以,经过大量的努力,并在论坛中进行搜索后,我发现了这个小小的快乐。

https://frontbackend.com/thymeleaf/hidden-inputs-in-thymeleaf

虽然它没有解决我的具体问题,但答案就在那里。我不会让你自己读它,因为我讨厌那样。

**** 答案***

{HTML 表单}

 <input class="form-control" type="text" id="blog-id"  th:field="*{id}">

{在控制器中}

@GetMapping(“/”) 公共字符串显示(模型模型){

   // Create the object first 
    BlogPost blogPost = new BlogPost();

   // Then add assign the value to object
    blogPost.setId( Value You Want To Add );

   // Then send the object that you added the id to as the attribute
    model.addAttribute("blogPost", blogPost);



    return "postBlog";
}

*** 结论*** 我将代码缩减为您需要看到的内容(在所有讨厌者开始之前)。这是我的解决方案,并且效果完美。不,我们都知道如何解决这个问题

干杯! Java埃迪


0
投票

因此,您需要做的是将 th:field 替换为 th:name 并添加 th:value,th:value 将具有您要传递的变量的值。

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

0
投票
 <form method="post"  th:action="@{'/details-view/'+ ${id}}">
 <fieldset>
   <div th:each="params: ${form.getHtmlNameType()}">
       <label>
           <span th:text="${params[0]}"></span>

          <input  th:field="${__${'form.'+params[1]}__}"  th:name="${params[1]}" th:type="${params[2]}"   >

       </label>
   </div>
   <input th:type="'hidden'"  th:value="${originalReferer}" name="originalReferer" >
  </fieldset>
  <button type="post" >Save</button>
</form>

-3
投票

有 2 种可能的解决方案:

1)你可以通过javascript在视图中设置它...(不推荐)

<input class="form-control"
       type="text"
       id="tbFormControll"
       th:field="*{clientName}"/>

<script type="text/javascript">
        document.getElementById("tbFormControll").value = "default";
</script>

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;
    }
    ...
}

希望有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.