0

似乎 JSP、Velocity、Freemarker 等可以提供所谓的“内部模板”:我可以描述一个外部模板,然后定义内部部分。像这样(简化):

主.jsp:

<html>
<body>
    <div class="container">
        <%@ include file="menu.jsp"%>
        <%@ include file="content.jsp"%>
    </div>
</body>
</html>

所以我可以定义 menu.jsp 和 content.jsp 并且一切正常。但是这里的外部块有对内部的引用。不太适合我。

我正在寻找 Java 的技术,它可以让我实现这样的东西:

some_block.jsp:

<template file="main_template.jsp">
<div>
    ... my content here
</div>
</template>

main_template.jsp:

<html>
<body>
    <div class="container">
        <inner_content />
    </div>
</body>
</html>

IE。反之亦然 -内部块引用了外部。JSP可以吗?如果没有 - 我应该在 Spring MVC 中使用什么?

编辑:为什么对我来说更舒服:当控制器收到请求时,它会检测应该呈现什么视图。所以我可以呈现,例如,反馈表:

反馈.jsp:

<template file="main_template.jsp">
    <form> ... feedback form content here ... </form>
</template>

或产品页面product.jsp

<template file="main_template.jsp">
    <div> ... product page content here ... </div>
</template>

并且不需要为每种页面描述页面结构,也不需要向外部模板传递任何参数来正确呈现内容。甚至不需要动态编译——所有页面只是一组隐含的预编译 servlet。

4

3 回答 3

0

我创建了一个您可能感兴趣的 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 上。

于 2012-11-28T23:28:15.990 回答
0

我找到了两种解决方案——一种很复杂,一种非常简单可爱:

第一个是 SiteMesh: http ://wiki.sitemesh.org/

第二个是来自 Claudius Hauptmann 的自定义标签: http ://code.google.com/p/jsp-decorator

两者都工作正常,所以我希望这对其他人有帮助。

于 2012-11-30T16:02:43.097 回答
0

使用 JSP TAG 文件很容易做到这一点。

首先创建您的模板标签:

/WEB-INF/tags/template/maintemplate.tag

<%@ tag body-content="scriptless" %>
<html>
<body>
    <div class="container">
        <jsp:doBody/>
    </div>
</body>
</html>

然后创建使用模板标签的 JSP 页面:

/some_page.jsp

<%@ page %>
<%@ taglib prefix="template" tagdir="/WEB-INF/tags/template" %>
<template:maintemplate>
    <div>
        ... my content here
    </div>
</template:maintemplate>

您的 JSP 页面使用 maintemplate.tag 并向其传递一个正文内容块。maintemplate.tag 选择在哪里呈现该正文内容。

于 2012-12-07T00:03:18.417 回答