search

Home  >  Q&A  >  body text

java - How does freemarker achieve this simple effect?

template

<!DOCTYPE html>
<html>
<head>
<#include "baseHeader.html" />
<#macro head ><#nested></#macro>
<#macro style ><#nested></#macro>
</head>

<body>
<#macro content >
    <#nested>
</#macro>
</body>

<#macro script >
    <#nested>
</#macro>
</html>

Page call

<#include "/layout/master.ftl" />
<@script>var a=1;</@script>

In the page generated as a result, the sentence var a=1; ran behind </html>, that is, at the end of the template, and did not appear in the corresponding part of the template. Location.

I have been doing it for several years.net, and I feel that it is natural for this kind of template to produce the effect I have.
But on java, it seems that it is not easy to achieve the effect I expected?

我想大声告诉你我想大声告诉你2736 days ago626

reply all(2)I'll reply

  • 阿神

    阿神2017-05-27 17:43:00

    Freemarker is not based on layout. It only does expression evaluation and text replacement, which cannot meet your requirements.

    As far as I know:

    1. Struts and Tiles plug-in can achieve the layout effect you want.

    2. Thymeleaf combined with thymeleaf-layout-dialect can also achieve the effect you want.

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-27 17:43:00

    The usage of macros is incorrect, please refer to the following

    <!-- 定义宏用 <#macro macroName parameterList> -->
    <#macro list title items>
        <p>${title?cap_first}:
        <ul>
            <#list items as item>
            <li>${item}
            </#list>
        </ul>
    </#macro>
    <!-- 调用宏用 <@macroName arguments> -->
    <@list items=animals title="animals"/>

    reply
    0
  • Cancelreply