ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Freemarker使用方法

2021-05-11 17:57:16  阅读:210  来源: 互联网

标签:var6 Freemarker 使用 fileOutputStream Throwable import null 方法 templatePath


1.添加依赖
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
</dependency>
2. 范例代码
import com.baomidou.mybatisplus.generator.config.ConstVal;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class FreemarkerTemplate {

    private Configuration configuration;

    // 推荐使用这种写法,因为Configuration类的构造方法已废弃
    public void writer(Map<String, Object> objectMap, String templatePath, String outputFile) throws Exception {
        Template template = this.configuration.getTemplate(templatePath);
        FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
        Throwable var6 = null;

        try {
            template.process(objectMap, new OutputStreamWriter(fileOutputStream, ConstVal.UTF8));
        } catch (Throwable var15) {
            var6 = var15;
            throw var15;
        } finally {
            if (fileOutputStream != null) {
                if (var6 != null) {
                    try {
                        fileOutputStream.close();
                    } catch (Throwable var14) {
                        var6.addSuppressed(var14);
                    }
                } else {
                    fileOutputStream.close();
                }
            }

        }

        log.info("模板:" + templatePath + ";  文件:" + outputFile);
    }

    // 测试方法
    public static void writerTest(Map<String, Object> objectMap, String templatePath, String templateFile, String outputFile) throws Exception {
        Configuration conf = new Configuration();
        //加载模板文件(模板的路径)
        conf.setDirectoryForTemplateLoading(new File(templatePath));
        // 加载模板
        Template template = conf.getTemplate(templateFile);
        FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
        Throwable var6 = null;

        try {
            template.process(objectMap, new OutputStreamWriter(fileOutputStream, "UTF-8"));
        } catch (Throwable var15) {
            var6 = var15;
            throw var15;
        } finally {
            if (fileOutputStream != null) {
                if (var6 != null) {
                    try {
                        fileOutputStream.close();
                    } catch (Throwable var14) {
                        var6.addSuppressed(var14);
                    }
                } else {
                    fileOutputStream.close();
                }
            }

        }

        log.info("模板:" + templatePath + ";  文件:" + outputFile);
    }

    public static void main(String[] args) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "zwc");
        map.put("age", "18");
        map.put("sex", "man");
        String fileName = "test";
        String templatePath = System.getProperty("user.dir")+"\\src\\main\\resources\\templates\\";
        String templateFileName = fileName + ".ftl";
        String outputFile = templatePath + "\\create\\"+ fileName +".html";
        writerTest(map, templatePath, templateFileName, outputFile);
    }
}
3. 扩展其余两种模板引擎(velocity(默认)、Beetl)
3.1 velocity
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.config.ConstVal;
import lombok.extern.slf4j.Slf4j;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

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

@Slf4j
public class VelocityTemplate {

    private VelocityEngine velocityEngine;

    public void writer(Map<String, Object> objectMap, String templatePath, String outputFile) throws Exception {
        if (!StringUtils.isEmpty(templatePath)) {
            Template template = this.velocityEngine.getTemplate(templatePath, ConstVal.UTF8);
            FileOutputStream fos = new FileOutputStream(outputFile);
            Throwable var6 = null;

            try {
                OutputStreamWriter ow = new OutputStreamWriter(fos, ConstVal.UTF8);
                Throwable var8 = null;

                try {
                    BufferedWriter writer = new BufferedWriter(ow);
                    Throwable var10 = null;

                    try {
                        template.merge(new VelocityContext(objectMap), writer);
                    } catch (Throwable var54) {
                        var10 = var54;
                        throw var54;
                    } finally {
                        if (writer != null) {
                            if (var10 != null) {
                                try {
                                    writer.close();
                                } catch (Throwable var53) {
                                    var10.addSuppressed(var53);
                                }
                            } else {
                                writer.close();
                            }
                        }

                    }
                } catch (Throwable var56) {
                    var8 = var56;
                    throw var56;
                } finally {
                    if (ow != null) {
                        if (var8 != null) {
                            try {
                                ow.close();
                            } catch (Throwable var52) {
                                var8.addSuppressed(var52);
                            }
                        } else {
                            ow.close();
                        }
                    }

                }
            } catch (Throwable var58) {
                var6 = var58;
                throw var58;
            } finally {
                if (fos != null) {
                    if (var6 != null) {
                        try {
                            fos.close();
                        } catch (Throwable var51) {
                            var6.addSuppressed(var51);
                        }
                    } else {
                        fos.close();
                    }
                }

            }

            log.info("模板:" + templatePath + ";  文件:" + outputFile);
        }
    }
}
3.2 Beetl
import lombok.extern.slf4j.Slf4j;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;

import java.io.FileOutputStream;
import java.util.Map;

@Slf4j
public class BeetlTemplate {

    private GroupTemplate groupTemplate;

    public void writer(Map<String, Object> objectMap, String templatePath, String outputFile) throws Exception {
        Template template = this.groupTemplate.getTemplate(templatePath);
        FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
        Throwable var6 = null;

        try {
            template.binding(objectMap);
            template.renderTo(fileOutputStream);
        } catch (Throwable var15) {
            var6 = var15;
            throw var15;
        } finally {
            if (fileOutputStream != null) {
                if (var6 != null) {
                    try {
                        fileOutputStream.close();
                    } catch (Throwable var14) {
                        var6.addSuppressed(var14);
                    }
                } else {
                    fileOutputStream.close();
                }
            }

        }

        log.info("模板:" + templatePath + ";  文件:" + outputFile);
    }
}

标签:var6,Freemarker,使用,fileOutputStream,Throwable,import,null,方法,templatePath
来源: https://blog.csdn.net/zhangwenchao0814/article/details/116664313

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有