How to pass an object from the controller to the view in SpringMVC?

The scenario is: I want to show a list of items on my view from the database. My controller picks up the data, passes it to the view which displays it. I am using a sub class of org.springframework.web.servlet.mvc.SimpleFormController:

@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {

ModelAndView view = new ModelAndView(“complaintListings”);
List<String> strings = new ArrayList<String>();
strings.add(“AAAAAAAA”);
strings.add(“BBBBBBB”);
strings.add(“CCCCCCCC”);
strings.add(“EEEEEEEE”);
strings.add(“FFFFFFFFF”);

//set the object to view
view.addObject(“testStrings”, strings);

return view;

}

The view looks like this:

<c:forEach var=”string” items=”${testStrings}”>
<c:out value=”${string}”/>
<br />
</c:forEach>

Its that simple!

Leave a Reply

Your email address will not be published. Required fields are marked *