百里香叶+;Spring:表单中的2个对象覆盖ID

百里香叶+;Spring:表单中的2个对象覆盖ID,spring,spring-boot,thymeleaf,Spring,Spring Boot,Thymeleaf,我正在使用弹簧靴(v2.0.1.RELEASE)。我在HTML模板中遇到问题,试图将表单中的两个不同对象发送到控制器。成功发送表单对象的所有属性,但两者的ID属性除外。两者都有一个名为ID的字段,当到达控制器时,该字段将被覆盖 这是HTML中的表单: <form action="#" th:action="@{/test}" method="post" class="form floating-label"> <div class="form-group">

我正在使用弹簧靴(v2.0.1.RELEASE)。我在HTML模板中遇到问题,试图将表单中的两个不同对象发送到控制器。成功发送表单对象的所有属性,但两者的ID属性除外。两者都有一个名为ID的字段,当到达控制器时,该字段将被覆盖

这是HTML中的表单:

<form action="#" th:action="@{/test}" method="post" class="form floating-label">
     <div class="form-group">

          <!-- TEST VARIABLES -->
          <input type="text" th:field="${test.id}" class="form-control"/>
          <input type="text" th:field="${test.nombre}" class="form-control"/>
          <input type="text" th:field="${test.formatotest.id}" class="form-control"/>
          <input type="text" th:field="${test.formatotest.nombre}" class="form-control"/>
          <input type="text" th:field="${test.activo}" class="form-control"/>

          <!-- USER VARIABLES-->
          <input type="text" th:field="${user.id}" class="form-control"/>
          <input type="text" th:field="${user.email}" class="form-control"/>
          <input type="text" th:field="${user.username}" class="form-control"/>

     </div>
     <div>
          <input type="submit" value="Send" class="btn btn-primary btn-raised" />
     </div>
     <br>
</form>
当我打印两个ID时,两个ID显示相同。我试着用th:value,name和id代替th:field,但还是失败了

第二次尝试HTML表单:

<form action="#" th:action="@{/test}" method="post" class="form floating-label">
    <div class="form-group">

         <!-- TEST VARIABLES -->
         <input type="hidden" th:value="${test.id}" name="id" id="id" class="form-control"/>
         <input type="hidden" th:value="${test.nombre}" name="nombre" id="nombre" class="form-control"/>
         <input type="hidden" th:value="${test.formatotest.id}" name="formatotest.id" id="formatotest.id" class="form-control"/>
         <input type="hidden" th:value="${test.formatotest.nombre}" name="formatotest.nombre" id="formatotest.nombre" class="form-control"/>
         <input type="hidden" th:value="${test.activo}" name="activo" id="activo" class="form-control"/>

         <!-- USER VARIABLES-->
         <input type="hidden" th:value="${user.id}" name="id" id="id" class="form-control"/>
         <input type="hidden" th:value="${user.email}" name="email" id="email" class="form-control"/>
         <input type="hidden" th:value="${user.username}" name="username}" id="username" class="form-control"/>

    </div>
    <div>
         <input type="submit" value="Send" class="btn btn-primary btn-raised" />
    </div>
    <br>
</form>



一个奇怪的细节是,它取决于我在HTML中首先输入的内容,这些内容将被设置为它们两个的集合。

我找到了anwser,它并没有以应有的干净方式出现,但工作正常。由于无法同时发送这两个对象,我创建了第三个包含这两个ID的对象,并将这三个对象传递给控制器,如下所示

包含两个ID的新对象:

package app.TestUser;

import app.hibernate.User;
import app.hibernate.Test;

public class TestUser {

    private String user_id;
    private String test_id;

    public TestUser() {
    }

    public TestUser(String user_id, String test_id) {
        this.user_id = user_id;
        this.test_id = test_id;
    }

    public String getUser_id() {
        return this.user_id;
    }

    public void setUser_id(String user_id) {
        this.user_id = user_id;
    }

    public String getTest_id() {
        return this.test_id;
    }

    public void setTest_id(String test_id) {
        this.test_id = test_id;
    }

}
控制器方法:

public ModelAndView showTest(TestUser testuser, Test test, User user) {

        user.setId(Integer.parseInt(testuser.getUser_id()));
        test.setId(Integer.parseInt(testuser.getTest_id()));

        ModelAndView testModel = new ModelAndView("test");

        testModel.addObject("user",user);
        testModel.addObject("test",test);

        return testModel;
    }
HTML格式:

<form action="#" th:action="@{/test}" method="post" class="form floating-label">
     <div class="form-group">

          <!-- TEST VARIABLES -->
          <input type="hidden" th:field="${testuser.test_id}"/>
          <input type="hidden" th:field="${test.nombre}"/>
          <input type="hidden" th:field="${test.formatotest.id}"/>
          <input type="hidden" th:field="${test.formatotest.nombre}"/>
          <input type="hidden" th:field="${test.activo}" name="activo"/>

          <!-- USER VARIABLES-->
          <input type="hidden" th:field="${testuser.user_id}"/>
          <input type="hidden" th:field="${user.email}"/>
          <input type="hidden" th:field="${user.username}"/>

     </div>
     <div>
          <input type="submit" value="Send" class="btn btn-primary btn-raised" />
     </div>
     <br>
</form>




当您试图保留th:字段但为name和id属性指定不同的值时会发生什么?例如,我已经尝试过了,但它仍然是一样的,它会覆盖它。我读过一些东西,当使用th:field时,name-value和id是ommited,当只有一个对象时使用。我真的迷路了…在表单中,尝试添加th:object=“${test}&&${user}”我尝试像您所说的那样在表单标记中添加th:object=“${test}&&${user}”,它显示了错误。[org.thymeleaf.exceptions.TemplateProcessingException:无法解析为表达式:“${test}&${user}”(模板:“index”-第100行,第67列)]。该注释不合法。
<form action="#" th:action="@{/test}" method="post" class="form floating-label">
     <div class="form-group">

          <!-- TEST VARIABLES -->
          <input type="hidden" th:field="${testuser.test_id}"/>
          <input type="hidden" th:field="${test.nombre}"/>
          <input type="hidden" th:field="${test.formatotest.id}"/>
          <input type="hidden" th:field="${test.formatotest.nombre}"/>
          <input type="hidden" th:field="${test.activo}" name="activo"/>

          <!-- USER VARIABLES-->
          <input type="hidden" th:field="${testuser.user_id}"/>
          <input type="hidden" th:field="${user.email}"/>
          <input type="hidden" th:field="${user.username}"/>

     </div>
     <div>
          <input type="submit" value="Send" class="btn btn-primary btn-raised" />
     </div>
     <br>
</form>