将JSP页面包含到另一个JSP页面中,如何避免多个HEAD / BODY段? [英] Including JSP page into another JSP page, how to avoid multiple HEAD / BODY sections?

查看:805
本文介绍了将JSP页面包含到另一个JSP页面中,如何避免多个HEAD / BODY段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将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 masterand 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆