• FreeMarker 语法 include 引用模板


    一、java 代码

    @Test
    public void testFreeMarker() throws Exception {
        //1、创建一个模板文件
        //2、创建一个Configuration对象
        Configuration configuration = new Configuration();
        //3、设置模板文件保存的目录
        configuration.setDirectoryForTemplateLoading(new File("E:/workspaces/fw-item-web/src/main/webapp/WEB-INF/ftl"));
        //4、模板文件的编码格式,一般就是utf-8
        configuration.setDefaultEncoding("utf-8");
        //5、加载一个模板文件,创建一个模板对象。
        Template template = configuration.getTemplate("student.ftl");
        //6、创建一个数据集。可以是pojo也可以是map。推荐使用map
        Map data = new HashMap<>();
        
        data.put("studnet", "studnet.ftl");
        data.put("hello", "hello.ftl");
        
        //7、创建一个Writer对象,指定输出文件的路径及文件名。
        Writer out = new FileWriter(new File("E:/freemarker/student.html"));
        //8、生成静态页面
        template.process(data, out);
        //9、关闭流
        out.close();
    }

    二、studnet.ftl

    <html>
    <head>
        <title>null</title>
    </head>
    <body>
        
        student.ftl 模板:<br>
        ${studnet}<br><br>
        
        引用模板测试:<br>
        hello.ftl 模板:<br>
        <#include "hello.ftl">
        
    </body>
    </html>

    三、hello.ftl

    ${hello}

    四、结果

  • 相关阅读:
    java 单链表 练习
    大问题-简明哲学导论
    git的常见错误
    python在Ubuntu添加模块搜索路径
    前端
    TCP/IP图解
    调试
    Design program
    算法
    面向对象-聚集,程序比较发现
  • 原文地址:https://www.cnblogs.com/fangwu/p/8696443.html
Copyright © 2020-2023  润新知