zoukankan      html  css  js  c++  java
  • Freemarker 入门

     

    新建maven项目,引入依赖

    <dependency>
      <groupId>org.freemarker</groupId>
      <artifactId>freemarker</artifactId>
      <version>2.3.23</version>
    </dependency>

    在resources下新建模板hello.ftl

    内容如下

    <#list userList as value>
      my name ${value}
    </#list>

    从模板读取数据,渲染到另外一个文件中

    Configuration configuration = new Configuration(Configuration.getVersion());
          Writer out = null;
          try {
              File srcFile = new File(TEMPLATE_PATH);
              System.out.println(srcFile.getAbsolutePath());
              System.out.println(TEMPLATE_PATH);
              // step2 获取模版路径
              configuration.setDirectoryForTemplateLoading(new File(TEMPLATE_PATH));
              // step3 创建数据模型
              Map<String, Object> dataMap = new HashMap<String, Object>();
              ArrayList userList = new ArrayList();
              userList.add("lgm");
              userList.add("wsf");
              userList.add("wsl");
              userList.add("zmy");
              userList.add("lsq");
              dataMap.put("userList", userList);
              // step4 加载模版文件
              Template template = configuration.getTemplate("hello.ftl");
              // step5 生成数据
              File docFile = new File(TEMPLATE_PATH + "\" + "hello.txt");
              out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
              // step6 输出文件
              template.process(dataMap, out);
          } catch (Exception e) {
              e.printStackTrace();
          } finally {
              try {
                  if (null != out) {
                      out.flush();
                  }
              } catch (Exception e2) {
                  e2.printStackTrace();
              }
          }

     

    替换后的结果

        my name lgm
      my name wsf
      my name wsl
      my name zmy
      my name lsq

    从字符串中读取模板

    StringTemplateLoader loader = new StringTemplateLoader();
    configuration.setTemplateLoader(loader);
    loader.putTemplate("tpm", "<#list userList as value> " +
    "   my name ${value} " +
    "</#list>");
    // step3 创建数据模型
    Map<String, Object> dataMap = new HashMap<String, Object>();
    ArrayList userList = new ArrayList();
    userList.add("lgm");
    userList.add("wsf");
    userList.add("wsl");
    userList.add("zmy");
    userList.add("lsq");
    dataMap.put("userList", userList);
    // step4 加载模版文件
    Template template = configuration.getTemplate("tpm");
    out = new StringWriter();
    // step6 输出文件
    template.process(dataMap, out);
    System.out.println(out.getBuffer().toString());

    替换后结果跟之前一样

    my name lgm
    my name wsf
    my name wsl
    my name zmy
    my name lsq

     

  • 相关阅读:
    MySQL5.6配置文件详解
    Mysql5.7.13忘记密码及创建用户
    CentOS7.2.1511 安装Mysql-5.7.13
    DAY01
    DAY01
    DAY01
    开启Python自动化运维之路
    Java知识点-----------单例设计模式
    Java知识点-----------重写Object的equals方法
    Linux基础06——压缩、解压、安装命令
  • 原文地址:https://www.cnblogs.com/liguangming/p/13161197.html
Copyright © 2011-2022 走看看