将JSP页面包含到另一个JSP页面中,如何避免多个HEAD / BODY段? [英] Including JSP page into another JSP page, how to avoid multiple HEAD / BODY sections?
问题描述
我想将JSP页面包含到另一个JSP页面中。假设我有 master.jsp
,包括 slave.jsp
。
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
有自己的< head>
部分,用于处理JavaScript和CSS ,是否有一种方法或可能是另一种方法来合并主
和奴隶
HEAD 部分成一个?同样应该对 BODY 部分做同样的事情。
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.
我最近一直在使用 sitemesh ,但我认为是为每个页面设置模板是非常不切实际的。
I have been using sitemesh recently but I think is quite impractical to setup a template for each page.
推荐答案
我通过传递参数包括页面时。
I went for this solution by passing a parameter when including the page.
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>
然后在 slave.jsp 中读取参数并自定义部件页面的呈现。
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>
不太好看,但工作正常。通过这种方式,我可以删除重复的 HEAD
和 BODY
部分。
not too nice to see but working. In this way I am able to remove the duplication of HEAD
and BODY
parts.
这篇关于将JSP页面包含到另一个JSP页面中,如何避免多个HEAD / BODY段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!