我创建了一个您可能感兴趣的 JSP 标签库:
http://www.inamik.com/projects/webframes/
以下是我将如何使用 WebFrames taglib 实现您的请求
请注意,这与您的示例具有精确的一对一相关性,但与您的示例不同,WebFrames 允许您创建无限的占位符,因此您可以拥有动态标题、css 包含、右频道内容、左-导航等
反馈.jsp
<%@ page language="java" %>
<%@ taglib prefix="wf" uri="/WEB-INF/tld/webframes.tld" %>
<wf:render file="main_template.jsp">
<wf:section name="inner_content">
<form> ... feedback form content here ... </form>
</wf:section>
</wf:render>
产品.jsp
<%@ page language="java" %>
<%@ taglib prefix="wf" uri="/WEB-INF/tld/webframes.tld" %>
<wf:render file="main_template.jsp">
<wf:section name="inner_content">
<div> ... product page content here ... </div>
</wf:section>
</wf:render>
main_template.jsp
<%@ page language="java" %>
<%@ taglib prefix="wf" uri="/WEB-INF/tld/webframes.tld" %>
<html>
<body>
<div class="container">
<wf:render section="inner_content" />
</div>
</body>
</html>
官方 WebFrames 示例
下面是网站上的完整示例集,展示了如何创建/填充多个占位符:
http://www.inamik.com/projects/webframes/examples/simpleexample.jsp
首先查看示例主页的源代码:
主要 http://www.inamik.com/projects/webframes/examples/simpleexample.jsp.txt
<%@ page language="java" %>
<%@ taglib prefix="wf" uri="/WEB-INF/tld/webframes.tld" %>
<wf:render file="frame.jsp">
<wf:section name="title">WebFrames Simple Example Page</wf:section>
<wf:section name="header" file="headersection.html" />
<wf:section name="footer" file="footersection.html" />
<wf:section name="body">
This page is a composite of the following sub-pages. Click the links below to see
the jsp/html that makes up each sub-page.
<UL>
<LI><A href="simpleexample.jsp.txt">simpleexample.jsp</A></LI>
<LI><A href="frame.jsp.txt">frame.jsp</A></LI>
<LI><A href="headersection.html.txt">headersection.html</A></LI>
<LI><A href="footersection.html.txt">footersection.html</A></LI>
</UL>
</wf:section>
</wf:render>
看看这如何在不定义布局的情况下定义内容部分。
如果您查看布局('frame.jsp'),您会发现它不知道要显示什么内容,它只是为内容创建占位符:
框架 http://www.inamik.com/projects/webframes/examples/frame.jsp.txt
<%@ page language="java" %>
<%@ taglib prefix="wf" uri="/WEB-INF/tld/webframes.tld" %>
<HTML>
<HEAD><TITLE><wf:render section="title" /></TITLE></HEAD>
<BODY>
<TABLE width="100%">
<!-- Header -->
<TR>
<TD>
<TABLE width="100%">
<TR>
<TD>
<wf:render section="header" />
</TD>
</TR>
</TABLE>
</TD>
</TR>
<!-- Body -->
<TR>
<TD>
<TABLE width="100%">
<TR>
<TD>
<wf:render section="body" />
</TD>
</TR>
</TABLE>
</TD>
</TR>
<!-- Footer -->
<TR>
<TD>
<TABLE width="100%">
<TR>
<TD>
<wf:render section="footer" />
</TD>
</TR>
</TABLE>
</TD>
</TR>
</BODY>
</HTML>
标题 http://www.inamik.com/projects/webframes/examples/headersection.html.txt
<!-- BEGIN headersection.html -->
<TABLE bgcolor="#00C0C0" width="100%">
<TR>
<TD nowrap="nowrap" align="LEFT">
<H1>WebFrames Sample Header</H1>
</TD>
</TR>
</TABLE>
<!-- END headersection.html -->
页脚 http://www.inamik.com/projects/webframes/examples/footersection.html.txt
<!-- BEGIN footersection.html -->
<TABLE bgcolor="#00C0C0" width="100%">
<TR>
<TD nowrap="nowrap" align="CENTER">
Copyleft (c) SampleFooter Inc.
</TD>
</TR>
</TABLE>
<!-- END footersection.html -->
这种模式提供了一个更加灵活的解决方案,可以创建可重用且更易于维护的组件。
笔记
这是基于 David Geary 早期的工作:
http://www.javaworld.com/javaworld/jw-12-2001/jw-1228-jsptemplate.html
另外,如果您愿意(或考虑)非 JSP 解决方案,我本着 PHP 的 Smarty 模板引擎的精神编写了一个基于 Java 的模板引擎
https://github.com/iNamik/iNamik-Template-Engine
我还有一个用于这个引擎的“Frames”标签库,它还没有在 GitHub 上。