码迷,mamicode.com
首页 > 其他好文 > 详细

用Main方法调用freemarker生成文件

时间:2017-01-28 14:23:53      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:调用   code   方法   bsp   array   static   main   exce   package   

MyGenerator.java

package com.comp.common;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

public class MyGenerator {

    public static void main(String[] args) {
        try {
            Map root = new HashMap();
            root.put("str", "hello world!");
            List data = new ArrayList();
            data.add("11");
            data.add("12");
            data.add("13");
            root.put("data", data);
            
            Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
            cfg.setDirectoryForTemplateLoading(new File("D:/web/template/"));
            cfg.setObjectWrapper(new DefaultObjectWrapper(Configuration.VERSION_2_3_23));
            Template temp = cfg.getTemplate("demo.ftl");
            String fileName = "demo.htm";
            File file = new File("D:/web/template/" + fileName);
            FileWriter fw = new FileWriter(file);
            BufferedWriter bw = new BufferedWriter(fw);
            temp.process(root, bw);
            bw.flush();
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

 

模板文件D:/web/template/demo.ftl

${str}
<#list data as row>
${row}
</#list>

 

生成的文件D:/web/template/demo.html

hello world!
11
12
13

 

用Main方法调用freemarker生成文件

标签:调用   code   方法   bsp   array   static   main   exce   package   

原文地址:http://www.cnblogs.com/modou/p/6354230.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!