简体   繁体   中英

jsp pass object to an include

Currently I am having a page with a small content need to be refreshed frequently.

For example: The jsp content is

  Chapter 1 :

  section abc:

  section def:

  section ghi: 

Assuming I have a class Chapter and its sections are parameters in it

 class Chapter{
     Section abc;
     Section def;
     Section ghi;
 }

I am using spring mvc, I am adding the following models

model.addAttribute("chapter", chapter);
model.addAttribute("abc", chapter.getAbc());
model.addAttribute("def", chapter.getDef());
model.addAttribute("ghi", chapter.getGhi());

While calling the main page initially I can load the individual sections by using the jsp:includes , while updating them independently I shall add that particular model to that particular jsp page and update it using ajax. This works fine.

But

The question is, How can I change the design so that can I manage the whole with just

model.addAttribute("chapter", chapter);

so that I can use the chapter object to get the abc,def,ghi values instead of explicitly passing them .

The problem is I am not able to pass the individual objects to the included jsps from the chapter object like ..

<jsp:include page="abc.jsp" >
     <jsp:param name="abc" value="${ chapter.abc}"/>
</jsp:include>

this is not possible. as I can pass only strings

small problem , long description .. hope I made my point.

You can only pass Strings as request parameters, but you can set any kind of object as request attributes . Request scope (not page scope) beans should also work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM