将JSP页面包含到另一个JSP页面中,如何避免多个HEAD/BODY部分?
我想将一个 JSP 页面包含到另一个 JSP 页面中。假设我有 master.jsp
,其中包含 slave.jsp
。
由于 slave.jsp
有自己的 部分来处理 JavaScript 和 CSS,是否有一种方法或另一种方法来合并
master 和
slave
HEAD 部分合并为一个?对于 BODY 部分也应该执行相同的操作。
我最近一直在使用 sitemesh,但我认为为每个页面设置一个模板是相当不切实际的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我通过在包含页面时传递参数来寻求此解决方案。
在 master.jsp 中
,然后在 slave.jsp 中读取参数并呈现页面的自定义部分。
不太好看到但工作。通过这种方式,我可以删除
HEAD
和BODY
部分的重复部分。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.您不能也不应该相互合并两个
文档。这会产生无效的输出。在 JSTL 的帮助下更好地包含 CSS/JS a href="http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/c/if.html" rel="nofollow noreferrer">
c:if
< /a> 或c:choose
标签。基本示例:
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:
在 sitemesh 之外,你就很不走运了。但是,如果您认为每页的设置不切实际,我会重新考虑您的设计。您的应用程序将有多少页面?
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?
您还可以扩展条件选项,并创建一个
meta.jsp
(例如),其中包含每个 head 元素的Map
- 元标记、css hrefs、脚本 hrefs,并使用 jsp 的名称作为该 Map 中的键。然后,您调用 request.getRequestURI(),并显示您在该键下放入地图中的所有内容。这不是一个非常漂亮的解决方案,但是有效。
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.