java - How does freemarker achieve this simple effect?
我想大声告诉你
我想大声告诉你 2017-05-27 17:41:00
0
2
587

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?

我想大声告诉你
我想大声告诉你

reply all(2)
阿神

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.

我想大声告诉你

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"/>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!