Search code examples
springspring-bootthymeleaf

Thymeleaf + Spring: 2 objects in form overwrite IDs


Im using Spring Boot (v2.0.1.RELEASE). Im having the problem in the HTML template, trying to send 2 different objects in a Form to the controller. All attributes form objects are sent successfully except from the ID attribute of both. Both has a field called ID with is being overwriten when arrives to the controller.

Thats the form in the 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>

Controller Function:

@PostMapping("/test")
   public ModelAndView showTest(Test test, User user) {

       System.out.println(" * User ID: ["+user.getId()+"] Test ID:["+test.getId()+"]");

       ModelAndView testModel = new ModelAndView("test");
       testModel.addObject("user",user);
       testModel.addObject("test",test);

       return testModel;
   }

When I print both IDs it shows same for both of them. I tried to use th:value, name nad id instead of th:field and it stills fail.

Second try of the HTML form:

<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>

A curious detail is that it depends on what input I put first in the HTML that will be the set to both of them.


Solution

  • I've found the anwser, not in the clean way it should be but it works fine. As I couldnt send both objects, I created a third object cointaining both ids, and pass those 3 objects to the controller as shown below.

    New object containing both ids:

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

    Controller method:

    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:

    <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>