Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring ThymileAF-将表数据(ArrayList)发布到另一个控制器_Spring_Thymeleaf - Fatal编程技术网

Spring ThymileAF-将表数据(ArrayList)发布到另一个控制器

Spring ThymileAF-将表数据(ArrayList)发布到另一个控制器,spring,thymeleaf,Spring,Thymeleaf,我很难在Thymeleaf POST中绑定ObjectList 我正在尝试使用Spring5和ThymeLeaf实现以下要求 1. User uploads a Excel File 2. Display data in HTML Table (For Java Script Data Validation) 3. Allow User to Delete Invalid Rows (from Inmemory User Specific ArrayList) 4. Display Remai

我很难在Thymeleaf POST中绑定ObjectList

我正在尝试使用Spring5和ThymeLeaf实现以下要求

1. User uploads a Excel File
2. Display data in HTML Table (For Java Script Data Validation)
3. Allow User to Delete Invalid Rows  (from Inmemory User Specific ArrayList)
4. Display Remaining Values
5. POST valid remaining values to Server
我计划在每行中添加一个删除按钮。以及一个Submit按钮,用于将所有剩余行保存在DB中

如何将每个列表转发到另一个控制器以执行删除操作和数据库保存

@PostMapping("/load-excel")
public ModelAndView loadFile(@RequestParam("fileName") MultipartFile file,
         @RequestParam("selectedApplicationName") String selectedApplicationName,RedirectAttributes redirectAttr,Model model) {

        List<EachRowinExcel> eachRowList = fileReaderService.convertExceltoObjList();
        ....

        modelAndView.addObject("eachRowList", eachRowList);

        return modelAndView;
}



 <tr th:each="eachRow : ${eachRowList}">
              <td th:text="${eachRow.column1}"></td>
              <td th:text="${eachRow.column2}"></td>
              <td th:text="${eachRow.column3}"></td>
              <td th:text="${eachRow.column4}"></td>
              <td th:text="${eachRow.column5}"></td>
              <td th:text="${eachRow.column6}"></td>
              <td th:text="${eachRow.column7}"></td>
              <!-- Special Columns -->
              <th:block th:each="customColumnValue:${eachRow.customColumnsList}">
                <td th:text="${customColumnValue}"></td>
              </th:block> 

            </tr>
更新2

对列标题采用了类似的方法。我在包装器类中有另一个列表对象。 它在初始加载时工作,但从POST控制器返回后,标头将丢失

  <thead>
    <tr>              
      <th scope="col">User Name</th>
      <th scope="col">Login</th>
      <th scope="col">Email</th>
      <th:block th:each="key, iter : ${form.customColumns}">
        <th th:text="${key}" scope="col"></th>
        <input type="hidden" th:field="${form.customColumns[__${iter.index}__]}" />
      </th:block> 
      <th scope="col">Action</th>
    </tr>
  </thead>
最后更新:


显然th:field输入标记不会绑定到ad部分,它不应该有输入字段LoL。在我在表格开始之前移动它之后,一切都按预期进行。

对于“提交”按钮,您需要一个环绕表格的表单。一个可行的解决方案是使用隐藏的输入字段来保存每个td的数据


提交表单后,您可以访问控制器中输入字段保存的数据,并将其保存在数据库中。

您需要将表放入表单中,表单将决定将向控制器的th:object字段发送什么。这在spring中称为命令对象。在本例中,假设eachRowList列表位于名为form的命令对象内

<form method="post"
        th:action="@{/mappingInYourController}"
        th:object="${form}">
<!-- Here goes your table -->
</form>

谢谢你在这个豪尔赫节目中的时间。我尝试了您的输入,但在POST方法中得到了空值。我已经添加了修改过的代码。糟糕的是,那些th:field标记必须在输入元素中。我会更新答案,希望它现在可以正常工作。此外,对于每行上的“删除”按钮,您很可能希望使用“按钮动作”属性来更改每个按钮的动作,以便您可以告诉控制器您选择删除的条目。您也可以在操作中使用[${iter.index}]部分来完成此操作。哇!!谢谢。工作得很好。几乎完成,除了表标题。我已经添加了更新3。我遗漏了什么吗?您正在使用th:field=${form…首先,对于th:fields,您应该使用*{},而不是${}语法。其次,这是因为*{}引用了表单的th:object。因为在您的示例中,这个th:object被称为form,所以您不需要使用*{}再次引用它,只有在使用${}时才需要使用。工作语法是th:field=*{customColumns[\uuuu${iter.index}\uuuuz]}。正如您所说,我以前确实尝试过这个方法,然后将其更改为显式引用。无论如何,两者都不起作用。表单包装类是否可以包含多个列表作为成员?感谢您的输入。我使用了隐藏属性。
  <thead>
    <tr>              
      <th scope="col">User Name</th>
      <th scope="col">Login</th>
      <th scope="col">Email</th>
      <th:block th:each="key, iter : ${form.customColumns}">
        <th th:text="${key}" scope="col"></th>
        <input type="hidden" th:field="${form.customColumns[__${iter.index}__]}" />
      </th:block> 
      <th scope="col">Action</th>
    </tr>
  </thead>
<input type="hidden" th:field="${eachRow.column1}"/>
<form method="post"
        th:action="@{/mappingInYourController}"
        th:object="${form}">
<!-- Here goes your table -->
</form>
<tr th:each="eachRow, iter : ${form.eachRowList}">
<!-- ... -->
<td> <input th:value="${eachRow.column1}" th:field="*{eachRowList[__${iter.index}__].column1}"/>
</td>
th:readonly="true"