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   疯狂的妞妞  阅读(575)  评论(0编辑  收藏  举报

(评论功能已被禁用)
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示