【问题标题】:Spring and thymeleaf set and get value春天和百里香叶集并获得价值
【发布时间】:2018-05-23 17:02:51
【问题描述】:

我将对象myObject 发送给查看,其中一个值使用post 方法放在form 内的input 中。如何设置要查看的值,然后通过提交此表单(但使用另一种对象类型)来获取它?

我尝试了类似的方法:

<form action="#" th:action="@{/myMethod}" th:object="${anotherObject}" method="post" xmlns="http://www.w3.org/1999/html">
<input  style="display:none" type="text" th:name="*{fieldFromAnotherObject}" th:value="${sendedObject.fieldFromSendedObject}"/>

但结果是这个 html 元素中的 name 字段为空:

(值正确)。然后我的后端中有null 作为fieldFromAnotherObject 字段anotherObject

【问题讨论】:

    标签: java spring spring-mvc spring-boot thymeleaf


    【解决方案1】:

    我不确定您为什么使用两个对象,一个用于绑定值以查看,一个用于提交,如果两者属于同一类,则一个对象就足够了。使用 th:field 您可以通过单个对象实现此目的。试试下面的代码,它应该可以工作。

      <form action="#" th:action="@{/myMethod}" th:object="${anotherObject}" method="post" >
      <input  style="display:none" type="text" th:field="*{fieldFromSendedObject}"/>
    

    如果两者属于不同的类对象,请尝试以下代码

      <input  style="display:none" type="text" th:name="anotherObject.fieldFromSendedObject" th:value="${sendedObject.fieldFromSendedObject}"/>
    

    【讨论】:

      猜你喜欢
      • 2015-12-09
      • 2014-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-01
      • 2017-10-18
      • 1970-01-01
      相关资源
      最近更新 更多