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 master
and 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.
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 JSTLc:if
orc:choose
tags.Basic example:
You could also extend the conditional option, and make a
meta.jsp
(for example), which contains aMap
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 callrequest.getRequestURI()
, and show whatever you have put in the map under that key. Not a very beautiful solution, but working.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?
I went for this solution by passing a parameter when including the page.
in master.jsp
and then in slave.jsp the parameter is read and the custom part of the page is rendered.
not too nice to see but working. In this way I am able to remove the duplication of
HEAD
andBODY
parts.