Spring-mvc bind arraylist in form

In my controller, I added an ArrayList model to my model with the attribute name "users".

Now I have looked around and this is the method I found (including the question here):

<form:form action="../user/edit" method="post" modelAttribute="users">
            <table>
                <c:forEach var="user" items="${users}" varStatus="counter">
                    <tr>
                        <td>
                                <form:input path="users[${counter.index}].age"/>
                        </td>
                        <td><button type="submit" name="updateId" id="Update" value="${user.id}">Update</button></td>
                    </tr>
                </c:forEach>
            </table>
        </form:form>

      

But when I load the JSP page I get:

.springframework.beans.NotReadablePropertyException: Invalid properties '[0]' from beanclass [java.util.ArrayList]: user bean '[0]' is not readable or has invalid getter: Does the getter return type match the setter parameter type?

So, as far as possible this is not the way to go, but in that case, how do I bind an arraylist so that I can edit the values?

+2


a source to share


1 answer


Try the following code:

<c:forEach var="user" items="${users}">
    <tr>
            <td><c:out value="${users.age}"/></td>
    </tr>
</c:forEach>

      



and make sure there is a custom arraylist in the query scope . Or use the following code:

<jsp:useBean id="users" scope="request" type="java.util.Collection" />

      

+1


a source







All Articles