FreemarkerUtil工具类

来源:互联网 发布:linux基础入门教程 编辑:程序博客网 时间:2024/06/07 01:05
package com.mmiku.basic.util;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Map;

import org.apache.commons.io.output.FileWriterWithEncoding;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class FreemarkerUtil {
    private static FreemarkerUtil util;
    private static Configuration cfg;
    private FreemarkerUtil() {
    }
    
    public static FreemarkerUtil getInstance(String pname) {
        if(util==null) {
            cfg = new Configuration();
            cfg.setClassForTemplateLoading(FreemarkerUtil.class, pname);
            cfg.setDefaultEncoding("utf-8");
            util = new FreemarkerUtil();
        }
        return util;
    }
    
    private Template getTemplate(String fname) {
        try {
            return cfg.getTemplate(fname);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 通过标准输出流输出模板的结果
     * @param root 数据对象
     * @param fname 模板文件
     */
    public void sprint(Map<String,Object> root,String fname) {
        try {
            Template template  = getTemplate(fname);
            template.setEncoding("utf-8");
            template.process(root, new PrintWriter(System.out));
        } catch (TemplateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    /**
     * 基于文件的输出
     * @param root
     * @param fname
     * @param outpath
     */
    public void fprint(Map<String,Object> root,String fname,String outpath) {
        try {
            Template template  = getTemplate(fname);
            template.setEncoding("utf-8");
            template.process(root, new FileWriterWithEncoding(outpath, "utf-8"));
        } catch (TemplateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

0 0
原创粉丝点击