如何正确地将页眉和页脚页面导入FreeMarker页面?

And*_*ili 1 spring freemarker spring-mvc

我正在使用将FreeMarker用于视图的Spring MVC应用程序。

我绝对是FreeMarker的新手,但我遇到以下问题:在我的项目中,我必须将3个文件组装成单个页面。

所以我有:

1)header.ftl代表我所有页面的标题,如下所示:

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js is-ie8"><![endif]-->
<!--[if gt IE 8]><!--><html class="no-js"><!--<![endif]-->
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Registrazione -  MY WEBSITE</title>
        <meta name="robots" content="noindex,nofollow">

        <link rel="stylesheet" href="resources/css/webfont.css">
        <link rel="stylesheet" href="resources/bootstrap/css/bootstrap.min.css">
        <link rel="stylesheet" href="resources/bootstrap/css/bootstrap-theme.min.css">
        <link rel="stylesheet" href="resources/plugins/bs-select/bootstrap-select.min.css">
        <link rel="stylesheet" href="resources/plugins/bs-dialog/bootstrap-dialog.min.css">
        <link rel="stylesheet" href="resources/css/style.css" />

    </head>

    <body id="main">

        <!-- versione per popup. non prendere in considerazione -->
        <!--
        <div class="container page-header">
            <h1>MY WEBSITE</h1>
        </div>
Run Code Online (Sandbox Code Playgroud)

2)footer.ftl代表我所有页面的页脚:

<script src="assets/bootstrap/js/bootstrap.min.js"></script>
<script src="assets/plugins/bs-select/bootstrap-select.min.js"></script>
    <script src="assets/plugins/bs-select/i18n/defaults-it_IT.min.js"></script>
    <script src="assets/plugins/bs-dialog/bootstrap-dialog.min.js"></script>
    <script src="assets/plugins/jq-form-validation/jquery.validation.js"></script>
    <script src="assets/js/functions.lib.js"></script>
    <script src="assets/js/form-validation.js"></script>

    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

3)然后,我有了特定的页面(名为myPage.ftl,它仅表示内容,如下所示:

<h1>This is the content</h1>
<p>Some inforamation</p>
Run Code Online (Sandbox Code Playgroud)

header.ftlfooter.ftl都到这个目录** \ WEB-INF \ freemarker的\布局**我的项目。

所以我的问题是:我怎么能导入header.ftl的内容以上内容myPage.ftlfooter.ftl的内容下myPage.ftl页?

Dav*_*wer 5

我使用用户定义的宏来进行页面布局,例如,让我们称呼您的布局standardPage.ftl

<#macro standardPage title="">
<!DOCTYPE html>
<html lang="en">
<head>
    <title>${title}</title>
</head>
<body>
    <#include "/include/header.ftl">    

    <#nested/>

    <#include "/include/footer.ftl">    
</body>
</html>
</#macro>
Run Code Online (Sandbox Code Playgroud)

然后,您可以在每个页面中调用此宏,例如homePage.ftl

<#include "/pages/standardPage.ftl" />

<@standardPage title="Home">
    <h1>Home Page</h1>
    <p>This bit replaces the nested tag in the layout</p>
</@standardPage>
Run Code Online (Sandbox Code Playgroud)