• 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

     

  • 相关阅读:
    Eclipse在线安装spring-tool-suit插件
    使用Eclipse构建Maven项目
    uwsgi+flask环境中安装matplotlib
    开启flask调试
    linux进入软连接所指向的原目录
    eclipse打不开,报错 "java was started with exit code=13"
    gnuplot 的安装
    使用tcp_probe时最初没有输出,先卸载后加载模块之后就有了。
    一个简单的socket程序运行与抓包查看
    如何查看文件是dos格式还是unix格式的?
  • 原文地址:https://www.cnblogs.com/liguangming/p/13161197.html
Copyright © 2020-2023  润新知