Java教程

Freemarker使用方法

本文主要是介绍Freemarker使用方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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);
    }
}
这篇关于Freemarker使用方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!