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

后端 未结 2 1393
小蘑菇
小蘑菇 2020-12-14 02:54

My problem is the following :

I\'ve 2 differents objects that I\'ve to fill from a single form.

With 1 object, I simply do in the newFoo.html:



        
相关标签:
2条回答
  • 2020-12-14 03:01

    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>
    
    0 讨论(0)
  • 2020-12-14 03:06

    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.

    0 讨论(0)
提交回复
热议问题