美文网首页
freemaker简要说明

freemaker简要说明

作者: 后知不觉1 | 来源:发表于2024-11-02 15:42 被阅读0次

freemaker是一个通用的模版引擎,快速创建想要的字符串。

主要过程

  • 配置
  • 创建数据
  • 模版配置
  • 数据与模版结合生产字符串

主要参考文档:http://freemarker.foofun.cn/ref_specvar.html
指令有许多扩展功能,请参考上述文档

1. 配置

不需要重复创建 Configuration 实例; 它的代价很高,(window ),尤其是会丢失缓存。Configuration 实例就是应用级别的单例。

// 新建
Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);

// 指定加载目录,有多种方式
// 直接指定文件绝对路径
cfg.setDirectoryForTemplateLoading(new File("/where/you/store/templates"));
// 通过class查找文件的base路径
cfg.setClassForTemplateLoading(class, "/where/you/store/templates")
// 通过class查找文件的base路径
cfg.setClassForTemplateLoading(class, "/where/you/store/templates")
setClassLoaderForTemplateLoading(class.getClassload(), "/where/you/store/templates")

// 文件格式
cfg.setDefaultEncoding("UTF-8");

// 遇到异常处理选项,默认即可
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

2. 创建数据

只支持一个变量传入,因此数据组装需要一个大对象传入

    Map<String, Object> map = new HashMap<>();
    map.put("tableName", ftlCommonTableDTO.getTableName());
    map.put("tableTitles", ftlCommonTableDTO.getTableTitles());
    map.put("tableContents", ftlCommonTableDTO.getTableContents());

3. 模版配置

3.1、if

变量是否存在判断

<#if myOptionalVar??>
    when-present
<#else>
    when-missing
</#if>

其他逻辑运算符都支持不展示

3.2、list

指令list 并且展开变量

<p>We have these animals:
<table border=1>
  <#list animals as animal>
    <tr><td>${animal.name}<td>
  </#list>
</table>

多层嵌套

<p>We have these animals:
<ul border=1>
  <#list animals as animal>
    <li>
      <#list type as animal.type>
        <li>${animal.name}</li>
      </#list>
    </li>
  </#list>
</ul>
3.3、内建函数

内建函数更像修饰符,修饰符修饰时能有不同的功能;
比如:

<li>${animal.name?upper_case}</li>  输出时大写 

其他指令参考http://freemarker.foofun.cn/dgui_quickstart_template.html#autoid_3

4. 数据与模版结合生产字符串

数据模型 (root) 和一个模板 (temp), 由模板的 process 方法完成的。

Template template = configuration.getTemplate("test", "UTF-8");
Writer out = new OutputStreamWriter(System.out);
temp.process(root, out);

// 这里System.out直接输出打印, 实际中需要用变量接收;这都需要使用到outputStream,可以用字节数组接收ByteArrayOutputStream

这会向你的终端输出你在模板开发指南部分的 第一个示例 中看到的内容。

Java I/O 相关注意事项:基于 out 对象,必须保证 out.close() 最后被调用。当 out 对象被打开并将模板的输出写入文件时,这是很电影的做法。其它时候, 比如典型的Web应用程序,那就 不能 关闭 out 对象。FreeMarker 会在模板执行成功后 (也可以在 Configuration 中禁用) 调用 out.flush(),所以不必为此担心。

请注意,一旦获得了 Template 实例, 就能将它和不同的数据模型进行不限次数 (Template实例是无状态的)的合并。此外, 当 Template 实例创建之后 test.ftl 文件才能访问,而不是在调用处理方法时。

demo

public static  String getContent(String ftlName,  Map<String, ? extends Object> map) {
        ByteArrayOutputStream baos = null;
        OutputStreamWriter osw = null;
        String content = "";

        try {
            Template template = configuration.getTemplate(ftlName, "UTF-8");

            baos = new ByteArrayOutputStream();
            osw = new OutputStreamWriter(baos);

            template.process(map, osw);

            byte[] bytes = baos.toByteArray();
            content = new String(bytes);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            if (osw != null) {
                try {
                    osw.close();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }

            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
        }

        return content;
    }

相关文章

网友评论

      本文标题:freemaker简要说明

      本文链接:https://www.haomeiwen.com/subject/ugcgdjtx.html