Technical blog

July 13, 2008

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

Filed under: java, spring — Tags: , — paawak @ 11:38

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!

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress