• java工具类-FreeMarker


    package com.huawei.it.citools.utils;

    import java.io.File;
    import java.io.IOException;
    import java.io.StringWriter;
    import java.util.Map;

    import org.apache.log4j.Logger;

    import com.huawei.it.citools.commons.ApplicationException;
    import com.huawei.it.citools.commons.CiConstants;

    import freemarker.template.Configuration;
    import freemarker.template.Template;
    import freemarker.template.TemplateException;
     
    public class FreeMarkerUtils
    {

        private static Logger LOGGER = Logger.getLogger (FreeMarkerUtils.class);

        private static FreeMarkerUtils freemarkerUtils = new FreeMarkerUtils();

        private Configuration configuration;

        private FreeMarkerUtils()
        {
            try
            {
                File currFile = new File ("");
                String templatePath = currFile.getCanonicalPath() + "/template/";

                LOGGER.info ("freemarker template file path : " + templatePath);

                configuration = new Configuration();
                File path = new File (templatePath);
                configuration.setDirectoryForTemplateLoading (path);
                configuration.setDefaultEncoding ("UTF-8");
            }
            catch (Exception e)
            {
                LOGGER.info ("freemarker configuration fail....." + e);

                throw new ApplicationException ("freemarker configuration fail.....", e);
            }
        }
     
        public String process (Map rootMap, String ftlName) throws IOException, TemplateException
        {
            StringWriter sw = new StringWriter();
            try {
                Template template = configuration.getTemplate (ftlName);
                template.process (rootMap, sw);
                String result = sw.toString();
                return result;
            } finally {
                sw.close();
            }
        }
        public static FreeMarkerUtils newInstance()
        {
            return freemarkerUtils;
        }
    }

  • 相关阅读:
    JSON格式
    多行写入
    文件对象write() and read()
    一个虚拟摄像头Filter(Virtual Cam Capture Filter)
    五十种最好的开源爬虫
    web scraper 里的 Element click 模拟点击「加载更多」
    介绍一款好用又易学的爬虫工具:web scraper
    安装宝塔面板后 ,centos系统 挂载硬盘 或者 数据盘和系统盘合并
    帝国CMS恢复搜索功能 增加搜索数据源设置教程
    安装帝国CMS步骤 和恢复数据
  • 原文地址:https://www.cnblogs.com/heling/p/3874719.html
Copyright © 2020-2023  润新知