How to pass two objects to use in a form using thymeleaf?

I don't think you need to use two th:objects. Just use th:value

<form th:action="@{/foo}" method="post">
      <input type="text" th:value="${foo.name}" name="name"/>
      <input type="text" th:value="${bar.status}" name="status"/>
      <button type="submit">Go</button>
</form>

I would think Spring is smart enough, on the controller side, to use its mapping techniques to map your fields to their proper command object, foo or bar.


i used a div tag to surround the form input for my second object and added a th:object..... the controller processed it and added it to the database.

<form method=post th:object="${object1}" >
   <div th:object="${object2}" >

      code......

   </div> 
   <input type="submit" />
</form>