views:

296

answers:

4

HI

I would like to include a jsp page into another jsp. Let's say that I have master.jsp that is including slave.jsp.

Since slave.jsp has its own set of section for dealing with Javascript and CSS. Is there a way or maybe another method to merge the master/slave HEADs section into one ? There will be also the same issue with the BODYs section.

I have been using sitemesh recently but I think is quite impractical to setup a template for each page.

A: 

Outside of sitemesh, you're pretty much out of luck. However I would reconsider your design if you think that the setup per page is impractical. How many pages will your app have?

noah
So I guess that the only solution is to split the slave.jsp page in two parts slave-head.jsp and slave-body. In this way I can just include one of each in the appropriate section.
dawez
Or maybe also using the <c:import... with a param to include head/body section. Guess I will go for this.
dawez
+1  A: 

You cannot and should not merge two <html> documents in each other. This would produce invalid output. Better include CSS/JS conditionally with help of JSTL c:if or c:choose tags.

Basic example:

<head>
    <script type="text/javascript" src="global.js"></script>
    <c:if test="${isAdminPage}">
        <script type="text/javascript" src="admin.js"></script>
    </c:if>
</head>
BalusC
Hmm, SO's code syntax highlighter goes mad.
BalusC
A: 

You could also extend the conditional option, and make a meta.jsp (for example), which contains a Map for each of the head elements - meta tags, css hrefs, script hrefs, and use the name of the jsp as a key in that Map. Then you call request.getRequestURI(), and show whatever you have put in the map under that key. Not a very beautiful solution, but working.

Bozho
+1  A: 

So far I decided to go for this solution:

in master.jsp

<head>
  blablabla
  <c:import url="slave.jsp">
    <c:param name="sectionName" value="HEAD" />
  </c:import>
</head>
<body>
  blablabla
  <c:import url="slave.jsp">
  </c:import>
</body>

and then in slave.jsp

<c:choose>
  <c:when test="${param.sectionName == 'HEAD'}">
     head section here [without the <HEAD> tags !]
  </c:when>
  <c:otherwise>
     body section here [without the <BODY> tags !]
  </c:otherwise>
</c:choose>

not too nice to see but working.

dawez