Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 from 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!

like image 631
JBoy Avatar asked Feb 24 '23 21:02

JBoy


1 Answers

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);

like image 93
andyb Avatar answered Mar 05 '23 01:03

andyb