Spring Boot rest API POST list of objects
In this post well 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. Show Spring MVC Project structure using Maven
Maven DependenciesJSTL 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. It adds the following jar- jstl-1.2.jarTable of contents
Spring MVC binding List example Required XML ConfigurationSince 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. Spring MVC binding List example Model classesList stores objects of the User class which is defined as given below. Following class acts a container for the List of User objects. public class UserListContainer { private ListSpring MVC binding List example Controller class@Controller public class UserController { @RequestMapping(value = "/getUsers", method = RequestMethod.GET) public String getUsers(Model model) throws Exception{ ListIn 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 ViewsIf 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"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
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"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
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. 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{ ListOnce 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 Recommendations for learning
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-
|