How to pass an object to Spring MVC3 view

I have a simple test project in Spring 3, basically a method inside the controller that retrieves the data from the arraylist and "should" pass it to the Thsi view: what the method looks like:

@RequestMapping(value="/showUsers") public String showUsers(Model model){ ArrayList<User> users=this.copy(); model.addAttribute(users); return "showUsers"; } 

And here is jsp (showUsers.jsp)

Both of them are executed without the logs or warnings that are displayed in the view, but without the data ArrayList<User>

 <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 tips? Thanks!

+4
source share
2 answers

The model documentation contains 2 methods for adding attributes to the model. You are using a version without a name, so Spring will use the generated name . I think this generated name is not what you think.

You can add a model using model.addAttribute("users", users);

+5
source

Thanks, I solved it like this:

 @RequestMapping(value="/showUsers") @ModelAttribute("users") public ArrayList<User> showUsers(){ return userList; } 
+3
source

All Articles