简体   繁体   中英

Spring MVC4 + Thymeleaf Form data to Controller

I am working on a Spring MVC4 application that uses Thymeleaf as a templating engine. 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? 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?

Note: the user object I am passing in the form is an instance of UserAccount.java that lives in the Model.

在此处输入图片说明

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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