How to pass an object to the view Spring MVC3
I have a simple test project in Spring 3, basically a method within the controller that fetches data f开发者_JAVA百科rom an arraylist and "should" pass them to a view Thsi is how the method looks like:
@RequestMapping(value="/showUsers")
public String showUsers(Model model){
ArrayList<User> users=this.copy();
model.addAttribute(users);
return "showUsers";
}
And here's the jsp (showUsers.jsp)
They both execute with no logs or warnings the view is displayed but without the ArrayList<User>
data's :(
<table align="center" border="1">
<tr>
<td>Nr:</td><td>Name:</td><td>Email</td><td>Modify?</td>
</tr>
<c:forEach var="user" items="${users}" varStatus="status">
<tr>
<td><c:out value="${status.count}"/></td><td><c:out value="${user.name}"/></td>
<td><c:out value="${user.email}"/></td><td>Modify</td>
</tr>
</c:forEach>
</table>
Any advice? Thank you!
The Model documentation lists 2 methods for adding attributes to a Model. You are using the version without supplying a name, so Spring will use a generated name. I think this generated name is not what you think it is.
You could add the model using model.addAttribute("users", users);
Thank you all, i solved it this way:
@RequestMapping(value="/showUsers")
@ModelAttribute("users")
public ArrayList<User> showUsers(){
return userList;
}
精彩评论