Passing MadelAndObject attribute to child jsp page

1811 views java
7

I am new to JSP and currently I am working on a spring MVC project. I have a jsp page index.jsp and I am passing the required values from my controller to index.jsp by ModelAndView. So I am doing something like

ModelAndView modelAndView = new ModelAndView("index");
m.addObject("packageList", packageList);
return modelAndView;

packageList is an object of PackageList class that has many attributes of type Map and List. With this logic I'm able to access all packageList attributes in the index.jsp but my index.jsp is too large so I want to break my jsp page in 3 parts. header.jsp, footer.jsp and list.jsp. and will include these 3 jsp pages in my index.jsp page. But I don't know how to access packageList object in all the 3 child jsp pages.

answered question

1 Answer

2

You can use jsp include to do the same.

The model is already in the request object. You should be able to access it from your included jsp. If that doesn't work, you will need to store it as a request attribute (you might be able to just reuse the one stored by Spring MVC)... then pass the request attribute name as a parameter to the included jsp:

<c:set var="myRequestModel" value="${model}" scope="request" />
<jsp:include ... >
   <jsp:param name="model" value="myRequestModel"/>
</jsp:include>

In your included jsp will use that name to access that model:-

${param.model.mykey} 

posted this

Have an answer?

JD

Please login first before posting an answer.