java - Spring 和 thymeleaf : Sending an object to a controller from a th:each table

标签 java html spring spring-mvc thymeleaf

我正在使用 th:each 属性和 Thymeleaf 制作一个经验数据表,我的目标是在每行中有一个提交按钮,单击该按钮时,将向我的 Controller 发送一个与我单击提交按钮的行相对应的体验对象。

我不知道出了什么问题,并且似乎无法在网上找到任何可以帮助解决此问题的内容。

这是我的网页代码部分:

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

这是我将其发送到的方法:

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

    }

最佳答案

您需要在发送输入时填写表单:

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

这将对用户隐藏您的对象数据,但当他们单击“提交”时,它将根据需要发送对象数据(而不是像您当前那样发送空表单)。

关于java - Spring 和 thymeleaf : Sending an object to a controller from a th:each table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30926136/

相关文章:

java - java中字符串数字的排序

javascript - 防止在隐藏和显示时推送元素 - jQuery

javascript - 我如何使用 jquery 根据 url 中的关键字更改 css 类

java - 如何让 Spring 忽略 @autowired jmsTemplate 字段以进行测试?

java - 如何写出好的删除数据的方法来纠正呢?

java - Vaadin 点击快捷方式征用

java - 是否丰富 :tab allow reRender attribute? 如果是 - 当丰富 :tab is selected? 时,它会在该选项卡上重新渲染组件吗

java - 如果我自动将 'http://' 连接到前面,Web 连接不起作用

html - 使用 vh 对齐的问题

java - 明确验证失败