Spring和Thymeleaf:从th:each table向控制器发送一个对象

时间:2015-06-18 21:30:29

标签: java html spring spring-mvc thymeleaf

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

我不知道什么是错的,似乎无法在网上找到任何帮助解决这个问题。

以下是我的网页代码部分:

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

    }

3 个答案:

答案 0 :(得分:3)

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

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

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

答案 1 :(得分:0)

作为@ Aeseir的回答,你需要注意“th:object”和“th:field” “field”可以是int,double,String,List,对象应该有forter字段的getter和setter。 在这种情况下

public class Experience{ String field; List<String> list; //getter() and setter() }

答案 2 :(得分:0)

我有同样的问题。 @Aeseir所说的对我有帮助,但不是:

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

我需要使用:

<input type="hidden" th:value="${experience.experienceDate}" name="someName"/>