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

18,266

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

Share:
18,266
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    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 clicked, will send an experience object to my controller that corresponds with the row I clicked the submit button in.

    I have no idea whats wrong and can't seem to find anything online to help with this problem.

    Here is my section of webpage code:

    <div th:unless="${#lists.isEmpty(borrower.experiences)}">
        <h2>List of Experiences</h2>
        <!--  <form ACTION="#" th:action="@{/initiate-edit}" th:object="${experience}"
          method="POST">-->
            <table id="your-table-id">
              <thead>
                <tr>
                  <td>Edit Buttons</td>
                    <th>Date Planted</th>
                    <th>Rating</th>
                    <th>Category</th>
                    <th>Dept</th>
                    <th>Resolution</th>
                    <th>Source</th>
                    <th>Last Update Date</th>
                    <th>Last Update Name</th>
                    <th>Comment</th>
                </tr>
              </thead>
              <tbody>
                  <tr th:each="experience : ${borrower.experiences}">       
                    <td>
                      <form ACTION="#" th:action="@{/initiate-edit}" 
                        th:object="${experience}" method="POST">
                        <!--<a th:href="@{/initiate-edit/}">CLICK</a>-->
                        <button type="submit">Submit</button>
                      </form>
                    </td>
                    <td th:text="${experience.experienceDate}">13/01/2014</td>
                    <td th:text="${experience.rating}">4</td>
                    <td th:text="${experience.categoryShortDesc}">Account and Billing</td>
                    <td th:text="${experience.deptDesc}">Account and Billing</td>
                    <td th:text="${experience.resolutionShortTx}">Account and Billing</td>
                    <td th:text="${experience.source}">Account and Billing</td>
                    <td th:text="${experience.lastUpdateDate}">Account and Billing</td>
                    <td th:text="${experience.lastUpdatedName}">Account and Billing</td>
                    <td th:text="${experience.commentsShort}">Account and Billing</td>    
                  </tr>             
                </tbody>
           </table>    
    </div>
    

    Here is the method I am sending it to:

    @RequestMapping(value = "/initiate-edit", method = RequestMethod.POST)
        public String initiateEdit(@AuthenticationPrincipal final User user, 
                                   @ModelAttribute("SpringWeb")CustomerExperience editableExperience, final Model model) {
    
            LOG.info("THIS IS A TEST!!!" + editableExperience.getSsn());
    
            model.addAttribute("editableExperience", editableExperience);
    
            return EDIT_PAGE;
    
        }