FreeMarker(十)Java工具类

FreeMarker会有多种应用场景:

1、常规的用法,就是传入一段字符串,进行数据渲染,然后返回一段字符串;

2、也会出现 “从文件读取模版”的需求(比如:我们的.ftl前端界面)。

 

FreeMarker是自带缓存设计的,一个Configuration,可以同时使用 “字符串模版” 和 “文件模版”,

但是,只有一种场景能触发缓存,这与Configuration 的 TemplateLoading配置有关,如果项目中同时存在两种模版,需要做好取舍。

 

简单地封装了一些常用代码,按需调整即可。(下面的写法,字符串模版,不会触发缓存)

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;

import com.sea.common.util.bean.Null;
import com.sea.common.util.core.Resource;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.Version;

/**
 * 较通用的FreeMarkers工具类
 * 
 * @author ChenSS 2017年10月19日
 * @since FreeMarker Version2.3.26
 */
public class FreeMarkers {
    private static Configuration config;
    private static final Version VERSION = new Version("2.3.23");
    private static final String FILE_ROOT = "tmp/";

    public static Configuration defaultConfiguration() {
        if (config == null) {
            synchronized (FreeMarkers.class) {
                if (config == null) {
                    try {
                        config = new Configuration(VERSION);
                        config.setNumberFormat("0.####");
                        config.setDateFormat("yyyy-MM-dd");
                        config.setTimeFormat("HH:mm:ss");
                        config.setDateTimeFormat("yyyy-MM-dd HH:mm:ss");
                        config.setBooleanFormat("true,false");
                        config.setWhitespaceStripping(true);
                        config.setClassicCompatible(true);
                        File file = new File(Resource.getResourcePath(FILE_ROOT));
                        config.setDirectoryForTemplateLoading(file);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return config;
    }

    public static void strToWriter(String tmp, Object model, Writer writer) throws Exception {
        new Template(Null.STRING, new StringReader(tmp), config).process(model, writer);
    }
    
    public static void fileToWriter(String tmp, Object model, Writer writer) throws Exception {
        config.getTemplate(tmp).process(model, writer);
    }

    public static String strToStr(String tmp, Object model) throws Exception {
        StringWriter out = new StringWriter();
        strToWriter(tmp, model, out);
        return out.toString();
    }

    public static String fileToStr(String tmp, Object model) throws Exception {
        StringWriter out = new StringWriter();
        config.getTemplate(tmp).process(model, out);
        return out.toString();
    }

    public static void fileToFile(String tmp, File file, Object model) throws Exception {
        FileWriter writer = new FileWriter(FileUtils.newFile(file));
        fileToWriter(tmp, model, writer);
    }

    public static void fileToFile(String tmp, String file, Object model) throws Exception {
        fileToFile(tmp, new File(file), model);
    }
    
    public static void readerToWrite(Reader reader, Writer out, String tmpName, Map<String, Object> params)
            throws Exception {
        new Template(tmpName, reader, config).process(params, out);
    }
}

 

posted on 2018-05-23 23:47  疯狂的妞妞  阅读(554)  评论(0编辑  收藏  举报

导航