简体   繁体   中英

How to pass Spring MVC model to controller from a loop in JSP

Regarding Spring MVC and JSP.

My JSP iterates over a list of users. Each user is displayed in a row of a table.

On each row is an Edit and a Delete button.

When either button is clicked I want to call the associated controller method passing the user model for the associated row.

To do this, I've added a form within the loop. However, when I click the button, the user model values are always null in the controller.

  • Note: I know I can do this by using the ID of the model but I am trying to pass the model itself. If it isn't possible I'd like to understand why not.

Here is my controller method:

@RequestMapping(value = "/user/delete", method = RequestMethod.POST)
public ModelAndView deleteUser(@ModelAttribute("user") User user) {
    ....
}

Here is my JSP

<c:forEach var="user" items="${users}">
    <form:form action="/" id="user${user.id}Form" method="post" commandName="user">
        <tr>
            <td class="col-xs-8 col-sm-8 col-md-8 col-lg-8">${user.id}: ${user.firstName} ${user.lastName}</td>
            <td class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
                <div class="btn-toolbar pull-right" role="btn-toolbar">
                    <div class="btn-group" role="group">
                        <input class="btn btn-default btn-default"
                               type="Submit"
                               value="Edit"
                               id="${user.id}"
                               onclick="document.getElementById('user${user.id}Form').action='${pageContext.request.contextPath}/user/updateLoad'">
                    </div>
                    <div class="btn-group" role="group">
                        <input class="btn btn-default btn-default"
                               type="Submit"
                               value="Delete"
                               id="${user.id}"
                               onclick="document.getElementById('user${user.id}Form').action='${pageContext.request.contextPath}/user/delete'">
                    </div>
                </div>
            </td>
        </tr>
    </form:form>
</c:forEach>

What would I need to do to pass the user model to the controller please?

I can get the values in the controller if I do the following, however, I don't consider this to be a practical solution because of the overhead.

    <c:forEach var="user" items="${users}">
        <form:form action="/" id="user${user.id}Form" method="post" commandName="user">
            <input type="hidden" name="id" value="${user.id}"/>
            <input type="hidden" name="firstName" value="${user.firstName}"/>
            <input type="hidden" name="lastName" value="${user.lastName}"/>
            <input type="hidden" name="phone" value="${user.phone}"/>
            <input type="hidden" name="address" value="${user.address}"/>

An @ModelAttribute on a method argument indicates the argument should be retrieved from the model. If not present in the model, the argument should be instantiated first and then added to the model. Once present in the model, the argument's fields should be populated from all request parameters that have matching names.

The WebDataBinder class matches request parameter names to model attribute fields by name. Matching fields are populated after type conversion (from String to the target field type) has been applied where necessary.

Your Case:

1.Spring first create the instance of Model User if it's not instantiated and then it tries to bind the data. Right?

2.Now Question is that in your case how spring will gonna match request parameter names to model attribute fields by name?

Spring creates the instance but it fails on Binding the data so you get a null instance. This is the reason why it can't be done with this approch.

I think the only way to do this thing is by using your second approach that you have already posted in your question.

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