Spring and Thymeleaf: Sending an object to a controller from a th:each table

前端 未结 3 1523
再見小時候
再見小時候 2021-01-21 18:31

I am making a table of experience data using the th:each attribute with Thymeleaf and my goal is to have a submit button in each row that, when

相关标签:
3条回答
  • 2021-01-21 18:47

    I had the same problem. What @Aeseir said it has helped me, but instead of :

    <input type="hidden" th:field="*{experienceDate}"/>
    

    I needed to use:

    <input type="hidden" th:value="${experience.experienceDate}" name="someName"/>
    
    0 讨论(0)
  • 2021-01-21 18:59

    As @Aeseir's answer, you need to pay attention to the th:object and th:field The field can be int, double, String, List and the object should have getter and setter for its field. In this case

    public class Experience{
         String field;
         List<String> list;
         //getter() and setter()
    }
    
    0 讨论(0)
  • 2021-01-21 19:01

    You need to fill your form with inputs as the inputs get sent:

    <form ACTION="#" th:action="@{/initiate-edit}" th:object="${experience}" method="POST">
         <input type="hidden" th:field="*{experienceDate}"/>
         <input type="hidden" th:field="*{rating}"/>
         <!-- ADD ALL THE OTHER FIELDS THAT ARE PART OF THE OBJECT -->
         <button type="submit">Submit</button>
    </form>
    

    This will hide your object data from user but when they click on submit, it will send the object data as required (rather than having empty form sent as you have it currently).

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