【问题标题】:Spring MVC4 + Thymeleaf Form data to ControllerSpring MVC4 + Thymeleaf 表单数据到控制器
【发布时间】:2017-03-19 20:25:32
【问题描述】:

我正在开发一个使用 Thymeleaf 作为模板引擎的 Spring MVC4 应用程序。我有一个包含团队成员信息的表格,该表格在每一行中都有一个嵌入的表格,带有 delete 按钮,允许逐行删除用户。下面是表格部分和它目前的样子的屏幕截图......

<table class="bordered">
    <thead>
        <tr>
            <th data-field="id">Name</th>
            <th data-field="name">Password</th>
            <th data-field="price">Email</th>
        </tr>
    </thead>
    <tbody>
        <tr th:each="user : ${users}">
            <td th:text="${user.userName}">Name</td>
            <td th:text="${user.password}">Password</td>
            <td th:text="${user.email}">Email</td>
            <td>
                <!-- Submit the user to be deleted -->
                <form th:object="${user}" class="col s12" action="/deleteUser" method="post">
                    <button class="btn waves-effect waves-light red col" type="submit" name="deleteUser">
                    Delete
                    </button>
                    <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
                </form>
            </td>
        </tr>
    </tbody>
</table>

当我单击 delete 按钮时,我的 TeamController 会收到请求,但所有实例变量都为空,我对 Spring MVC4 和 Thymeleaf 还很陌生,所以不确定这是否是一种有效的方法?我假设表单中的对象是对行中用户对象的引用,所以不确定为什么它在控制器端为空?

注意:我在表单中传递的用户对象是位于模型中的 UserAccount.java 的一个实例。

【问题讨论】:

    标签: spring model-view-controller thymeleaf


    【解决方案1】:

    能够通过对我的表单进行以下更新来修复它...

    <td>
    <!-- Submit the user to be deleted -->
    <form th:object="${UserAccount}" class="col s12" th:action="@{/deleteUser}" method="post">
        <button class="btn waves-effect waves-light red col" type="submit" name="deleteUser">Delete</button>
        <input type="hidden" id="userName" name="userName" th:value="${user.userName}" />
        <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
    </form>
    

    【讨论】:

      猜你喜欢
      • 2019-07-05
      • 2018-11-17
      • 2021-01-07
      • 1970-01-01
      • 1970-01-01
      • 2022-08-23
      • 1970-01-01
      • 2018-03-10
      • 1970-01-01
      相关资源
      最近更新 更多