In this post we’ll see how to bind a list of objects in Spring MVC so that the objects in that List can be displayed in the view part.
Spring MVC Project structure using Maven
- Please refer Spring Web MVC Example With Annotations for getting the project structure using Spring XML configuration.
- Please refer Spring Web MVC Java Configuration Example for getting the project structure using Spring Java configuration.
Maven Dependencies
JSTL tags are also used in this Spring MVC example for binding list of objects so you need to add the following Maven dependency for JSTL apart from Spring dependencies.
<dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency>
It adds the following jar-
jstl-1.2.jar
Spring MVC binding List example – Required XML Configuration
Since JSTL tags are used in JSP so you need your view to resolve to JstlView, for that you need to add viewClass property in the bean definition for InternalResourceViewResolver in your DispatcherServlet configuration.
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean>
Spring MVC binding List example – Model classes
List stores objects of the User class which is defined as given below.
public class User { private String firstName; private String lastName; private String email; public User() {} public User(String firstName, String lastName, String email) { this.firstName = firstName; this.lastName = lastName; this.email = email; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
Following class acts a container for the List of User objects.
public class UserListContainer { private List<User> users; public List<User> getUsers() { return users; } public void setUsers(List<User> users) { this.users = users; } }
Spring MVC binding List example – Controller class
@Controller public class UserController { @RequestMapping(value = "/getUsers", method = RequestMethod.GET) public String getUsers(Model model) throws Exception{ List<User> users = getListOfUsers(); UserListContainer userList = new UserListContainer(); userList.setUsers(users); model.addAttribute("Users", userList); return "showUsers"; } // Dummy method for adding List of Users private List<User> getListOfUsers() { List<User> users = new ArrayList<User>(); users.add(new User("Jack", "Reacher", "abc@xyz.com")); users.add(new User("Remington", "Steele", "rs@cbd.com")); users.add(new User("Jonathan", "Raven", "jr@sn.com")); return users; } }
In the controller class there is a handler method getUsers() where a list of users is created and set to the UserListContainer which in turn is added as an attribute to the Model. Logical view name returned from the method is showUsers which resolves to a JSP at the location WEB-INF\jsp\showUsers.jsp.
Spring MVC binding List example – Views
If you just want to iterate the list and show the object fields then you can use the given JSP.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <html> <head> <meta charset="ISO-8859-1"> <title>Spring MVC List of objects display</title> </head> <body> <table> <tr> <th>First Name</th> <th>Last Name</th> <th>Email</th> </tr> <c:forEach items="${Users.users}" var="user" varStatus="tagStatus"> <tr> <td>${user.firstName}</td> <td>${user.lastName}</td> <td>${user.email}</td> </tr> </c:forEach> </table> </body> </html>
If you want to iterate the list, show the object fields and want to bind the List of objects again to modelAttribute then you can use the following JSP.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <html> <head> <meta charset="ISO-8859-1"> <title>Spring MVC List of objects display</title> </head> <body> <form:form method="POST" action="saveUsers" modelAttribute="Users"> <table> <tr> <th>First Name</th> <th>Last Name</th> <th>Email</th> </tr> <c:forEach items="${Users.users}" var="user" varStatus="tagStatus"> <tr> <td><form:input path="users[${tagStatus.index}].firstName" value="${user.firstName}" readonly="true"/></td> <td><form:input path="users[${tagStatus.index}].lastName" value="${user.lastName}" readonly="true"/></td> <td><form:input path="users[${tagStatus.index}].email" value="${user.email}" readonly="true"/></td> </tr> </c:forEach> </table> <input type="submit" value="Save" /> </form:form> </body> </html>
In this JSP Spring form tags are used for Spring MVC form fields and for looping the List JSTL tag is used. For these tag libraries following lines are added in the JSP.
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
To verify that the List of users is added to the Model and can be accessed in the handler method you can add the following method in the Controller class.
@RequestMapping(value = "/saveUsers", method = RequestMethod.POST) public void saveUsers(@ModelAttribute("Users") UserListContainer userList) throws Exception{ List<User> users = userList.getUsers(); for(User user : users) { System.out.println("First Name- " + user.getFirstName()); } }
Once the application is deployed it can be accessed using the URL - http://localhost:8080/spring-mvc/getUsers
Just showing the object fields
Showing the object fields and binding to Model
That's all for this topic Spring MVC - Binding List of Objects Example. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Spring Tutorial Page
Related Topics
You may also like-