简体   繁体   English

Spring MVC4 + Thymeleaf表单数据发送到控制器

[英]Spring MVC4 + Thymeleaf Form data to Controller

I am working on a Spring MVC4 application that uses Thymeleaf as a templating engine. 我正在使用Thymeleaf作为模板引擎的Spring MVC4应用程序上工作。 I have a table with team member information that has an embedded form in each row with a delete button to allow deleting a user by row. 我有一个包含团队成员信息的表,该表在每行中都有一个嵌入式表单,并带有删除按钮,以允许逐行删除用户。 below is the table section and a screenshot of what it currently looks like... 以下是表格部分和当前屏幕截图...

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

在此处输入图片说明

When I click the delete button my TeamController picks up the request but all the instance variables are null, I'm fairly new to Spring MVC4 and Thymeleaf so not sure if this is a valid approach or not? 当我单击删除按钮时, TeamController接收到请求,但所有实例变量均为null,这对Spring MVC4和Thymeleaf还是很陌生,所以不确定这是否有效吗? I am assuming that the object in the form is a reference to the user object in the row so not sure why its coming null on the controller side? 我假设表单中的对象是对行中用户对象的引用,所以不确定为什么它在控制器端为null?

Note: the user object I am passing in the form is an instance of UserAccount.java that lives in the Model. 注意:我以表单形式传递的用户对象是位于模型中的UserAccount.java的实例。

在此处输入图片说明

Was able to fix it with below updates to my form... 能够通过以下对我表格的更新对其进行修复...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM