Spring和Thymeleaf:从一个th:每个表发送一个对象到控制器 [英] Spring and Thymeleaf: Sending an object to a controller from a th:each table

查看:137
本文介绍了Spring和Thymeleaf:从一个th:每个表发送一个对象到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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

 < div th:unless =$ {#lists.isEmpty(borrower.experiences)}> 
< h2>经验列表< / h2>
<! - < form ACTION =#th:action =@ {/ initiate-edit}th:object =$ {experience}
method =POST> ; - >
< table id =your-table-id>
< thead>
< tr>
< td>编辑按钮< / td>
< th>植入日期< / th>
< th>评分< / th>
第< th>类别< / th>
th部门< / th>
< th>分辨率< / th>
< th>来源< / th>
< th>上次更新日期< / th>
< th>上次更新名称< / th>
< th>评论< / 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 /}>点击< / a> - >
< button type =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}>帐户和帐单< / td>
< td th:text =$ {experience.deptDesc}>帐户和帐单< / td>
< td th:text =$ {experience.resolutionShortTx}>帐户和帐单< / td>
< td th:text =$ {experience.source}>帐户和帐单< / td>
< td th:text =$ {experience.lastUpdateDate}>帐户和帐单< / td>
< td th:text =$ {experience.lastUpdatedName}>帐户和帐单< / td>
< td th:text =$ {experience.commentsShort}>帐户和帐单< / td>
< / tr>
< / tbody>
< / table>
< / div>

以下是我发送给它的方法:

@ pre> @RequestMapping(value =/ initiate-edit,method = RequestMethod.POST)
public String initiateEdit(@AuthenticationPrincipal final用户用户,
@ ModelAttribute(SpringWeb)CustomerExperience editableExperience,final Model模型){

LOG.info(This is a TEST !!!+ editableExperience.getSsn());

model.addAttribute(editableExperience,editableExperience);

返回EDIT_PAGE;



解决方案

当输入发送时填入表单:

 < form ACTION =#th:action =@ {/ initiate-edit}th:object =$ {experience}method =POST> 
< input type =hiddenth:field =* {experienceDate}/>
< input type =hiddenth:field =* {rating}/>
<! - 将所有其他字段添加为对象的一部分 - >
< button type =submit>提交< / button>
< / form>

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


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;

    }

解决方案

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

这篇关于Spring和Thymeleaf:从一个th:每个表发送一个对象到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆