2

I am creating CRUD application in Spring MVC 3. Every thing is working fine apart from the edit page. I want my edit page to be populated with with some tags with relevant database generated values, so I can see, edit if I want and save them. I passed list of values in modelAttribute to share with the edit.jsp. I can retrieve them values on edit.jsp, however I am unable to put those values in tag, as it doesn't have a value attribute. Please help.

Sharing the code below----------------------------

    enter code here
<form:form id="form5" commandName="user" action="modify.htm">
<table>
    <tr>
        <th>Name</th>
        <th>Age</th>
    </tr>
    <c:forEach items="${userList}" var="user">
        <tr>
            <td> <form:input path="name" value="${user.name}"/></td>
            <td><form:input path="age" value="{user.age}"/></td>
        </tr>
    </c:forEach>
    <tr><td><button type="button" onclick="modify()">Save</button></td></tr>
</table>
</form:form>

1 Answer 1

5

There is no value attribute for form:input (as you guessed). The data binding path has to be mentioned in path attribute only. So, as per my understanding of your problem, the solution is as below:

You have User class:

public class User{
    String name;
    String age;
    //Getter setters..
}

You need a command/model object class to hold list of UserS so can be used in view (as below)

public class Users {
    List<User> userList; 
    //Getter setter..
} 

Your controller handler method might add Users to the model

Users users = getUsers(); // Get them from somewhere(DB) and wrap in Users object
model.addAttribute("users", users);

Use in jsp as below:

<form:form id="form5" modelAttribute="users" action="modify.htm">
    <table>
        <tr>
            <th>Name</th>
            <th>Age</th>
        </tr>
       <c:forEach items="${users.userList}" var="user" varStatus="i">
            <tr>
                <td><form:input path="userList[${i.index}].name" /></td>
                <td<form:input path="userList[${i.index}].age" /></td>
            </tr>
       </c:forEach>
       <tr><td><button type="button" onclick="modify()">Save</button></td></tr>
    </table>
</form>
Sign up to request clarification or add additional context in comments.

2 Comments

I understood what you have suggested, but at the moment I am getting java.lang.NumberFormatException. I look into it a bit later, and I will reply back about the result. Thanks "Vinay" for your valuable suggestion.
Couldn't you use user.name instead of userList[${i.index}].name?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.