public static void main(String[] args) throws IOException, TemplateException {
        //1.创建一个Configuration对象
        Configuration configuration = new Configuration(Configuration.getVersion());
        //2.设置模板文件使用的字符集
        configuration.setDefaultEncoding(\"utf-8\");
        //3.设置模板文件所在的目录
        configuration.setDirectoryForTemplateLoading(new File(\"F:\\\\IdeaProjects\\\\spring-data-redis\\\\freemarker_01\\\\src\\\\main\\\\resources\"));
        //4.加载一个模板,创建一个模板对象
        Template template = configuration.getTemplate(\"test.ftl\");
        //5.创建一个模板使用的数据集
        Map map = new HashMap();
        map.put(\"name\", \"程序员\");
        map.put(\"message\", \"欢迎来到fr

/dependency>
(2)创建模板文件
模板文件中四种元素

1、文本,直接输出的部分

2、注释,即<#--...-->格式不会输出

3、插值(Interpolation):即${..}部分,将使用数据模型中的部分替代输出

4、FTL指令:FreeMarker指令,和HTML标记类似,名字前加#予以区分,不会输出。

test.ftl

<html>
<head>
    < charset=\"utf-8\">
    < >Freemarker入门小Demo </ >
</head>
<body>
<#--我只是一个注释,我不会有任何输出  -->
${name},你好。${message}<br/>
</body>
</html>

eemarker静态页面\");
        //6.创建输出流
        FileWriter writer = new FileWriter(\"f:\\\\test.html\");
        //7.输出模板文件
        template.process(map, writer);
        //8.关闭流
        writer.close();
    }

收藏 打印