Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including JSP page into another JSP page, how to avoid multiple HEAD / BODY sections?

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

As slave.jsp has its own <head> section for dealing with JavaScript and CSS, is there a way or maybe another method to merge the masterand slave HEADs section into a single one? Also the same should done for the BODYs section.

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

like image 487
dawez Avatar asked Oct 20 '25 15:10

dawez


2 Answers

I went for this solution by passing a parameter when including the page.

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 the parameter is read and the custom part of the page is rendered.

<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. In this way I am able to remove the duplication of HEAD and BODY parts.

like image 114
dawez Avatar answered Oct 24 '25 16:10

dawez


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>
like image 44
BalusC Avatar answered Oct 24 '25 17:10

BalusC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!