spring - ThymeLeaf - 将表数据(ArrayList)发布到另一个 Controller

标签 spring thymeleaf

我很难在 Thymeleaf POST 中绑定(bind) ObjectList。

我正在尝试使用 Spring 5 和 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

我计划在每一行中添加一个删除按钮。还有一个“提交”按钮,用于保存数据库中所有剩余的行。

如何将每个RowList转发到另一个 Controller (用于删除操作和数据库保存)。

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

更新1:

修改 View

<form action="#" th:action="@{/access/delete}" th:object="${form}" method="post">
    <table id="accessRequestDataTable"  class="display compact" style="width:100%">
      <thead>
        <tr>
          <!-- Headers -->       
        </tr>
      </thead>
      <tbody>
     <tr th:each="eachRow, iter : ${form.eachRowList}">
          <td  th:text="${eachRow.accessReqeustCounter}" th:field="*{eachRowList[__${iter.index}__].accessReqeustCounter}"></td>
          <td  th:text="${eachRow.accessReqeustID}" th:field="*{eachRowList[__${iter.index}__].accessReqeustID}"></td>
          <td  th:text="${eachRow.accessRequestType}" th:field="*{eachRowList[__${iter.index}__].accessRequestType}"></td>
          <td  th:text="${eachRow.userProfile}" th:field="*{eachRowList[__${iter.index}__].userProfile}"></td>
          <td  th:text="${eachRow.userFinalName}" th:field="*{eachRowList[__${iter.index}__].userFinalName}"></td>
          <td  th:text="${eachRow.userLoginName}" th:field="*{eachRowList[__${iter.index}__].userLoginName}"></td>
          <td  th:text="${eachRow.userEmail}" th:field="*{eachRowList[__${iter.index}__].userEmail}"></td>

          <td>              
            <button class="btn btn-danger btn-sm" 
            type="submit" value="submit">Delete</button>             
          </td>
        </tr>        

      </tbody>
    </table>
  </form>

POST Controller

@RequestMapping(value = "/access/delete", method = RequestMethod.POST)
public ModelAndView deleteUserFromTable(@ModelAttribute("form") EachRowListWrapper eachRowListWrapper){
    System.out.println(eachRowListWrapper.getEachRowList().size());
    ModelAndView modelAndView = new ModelAndView();
    eachRowListWrapper.getEachRowList().remove(0);
    modelAndView.setViewName("access-table");
    return modelAndView;        
}

更新2

对列标题采用类似的方法。我在包装类中有另一个 List 对象。 它在初始加载时工作,但从 POST Controller 返回后会丢失 header 。

  <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 输入标记不会在 thead 部分中绑定(bind)(它不应该有输入字段 LoL)。在表格开始之前移动它后,一切都按预期工作。

最佳答案

您需要将表格放入表单中,该表单将确定将在其 th:object 字段中发送到 Controller 的内容。这在 spring 中被称为命令对象。在此示例中,我们假设您的“eachRowList”列表位于名为“form”的命令对象内。

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

您还必须指定哪些数据将放置在您在 th:object 中定义的对象内,以便可以将其发送到 Controller 。这是通过以下方式通过输入 th:fields 完成的(以其中一行为例):

<tr th:each="eachRow, iter : ${form.eachRowList}">
<!-- ... -->
<td> <input th:value="${eachRow.column1}" th:field="*{eachRowList[__${iter.index}__].column1}"/>
</td>

请注意,当您使用 *{} 访问属性时,您正在引用您定义为表单的 th:object 的属性。

现在,例如,如果您使用按钮提交表单,您的 Controller 将能够接收命令对象“form”,其中包含eachRowList列表,并且列表的元素将是表输入上的元素。如果您不希望此输入在表中内联编辑,您可以这样做:

th:readonly="true"

关于spring - ThymeLeaf - 将表数据(ArrayList)发布到另一个 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56531796/

相关文章:

java - 在我的 servlet 中使用 thymeleaf,找不到 html 模板文件

java - 在 Spring Security 中添加新用户

Spring Hadoop |作业未出现在作业跟踪器 GUI 上

spring-boot - 在 SpringBoot 上使用 HttpClient/MultipartRequestEntity 检查进度

javascript - Freemarker 模板引擎中可以使用 Javascript 内联吗?

java - Spring Thymeleaf Hibernate 验证自定义消息在页面上不可用

spring - Spring MVC Controller 如何绑定(bind)到 JSP 页面?

java - 你能在同一个 java 构建中同时拥有 maven 和 gradle 吗?

java - 如何在Spring中的TestContext中使用服务器数据库而不是内存中的h2数据库?

java - 如何从公共(public)源文件获取thymeleaf变量