Freemarker 封装接口-java

  • Maven依赖
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-freemarker</artifactId>
   <version>2.4.3</version>
</dependency>
  • yml配置
 # 配置 freemarker 模版引擎 (生成页面)
  freemarker:
    enabled: true #是否启用freemarker
    template-loader-path: classpath:/ftl/ #设定模板的加载路径,多个以逗号分隔
    suffix: .ftl #设定模板的后缀
    content-type: text/html
    check-template-location: true #是否检查模板位置是否存在
    cache: false #是否启用模板缓存
    charset: UTF-8 #模板编码
  • 接口代码

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

/**
 * className    FreemarkerService
 * author       tomy
 * createTime   2021/7/20 17:40
 * description  生成静态页面service
 */
public interface FreemarkerService {

    /**
     * 生成静态页面
     *
     * @param ftlName 获取模板
     * @param params  数据模型
     * @param outFile 输出文件
     * @return 返回
     */
    boolean createStaticHtml(String ftlName, Map<String, Object> params, File outFile);

    /**
     * 生成静态数据
     *
     * @param ftlName 获取模板
     * @param params  数据模型
     * @return 返回
     * @throws Exception 异常
     */
    String createStaticData(String ftlName, Map<Object, Object> params) throws Exception;
}
  • 实现代码

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * className    FreemarkerServiceImpl
 * author       tomy
 * createTime   2021/7/20 17:40
 * description  生成静态页面service实现
 */
@Service
public class FreemarkerServiceImpl implements FreemarkerService {

    @Resource
    private Configuration configuration;

    @Override
    public boolean createStaticHtml(String ftlName, Map<String, Object> params, File outFile) {
        InputStream inputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            //获取模板
            Template template = configuration.getTemplate(ftlName);
            // 静态化页面内容
            String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, params);
            inputStream = IOUtils.toInputStream(content, "UTF-8");
            // 输出文件
            FileUtils.forceMkdirParent(outFile);
            fileOutputStream = new FileOutputStream(outFile);
            IOUtils.copy(inputStream, fileOutputStream);
            return true;
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
            return false;
        } finally {
            // 关闭流
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (fileOutputStream != null) {
                        try {
                            fileOutputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    @Override
    public String createStaticData(String ftlName, Map<Object, Object> params) throws Exception {
        try {
            //获取模板
            Template template = configuration.getTemplate(ftlName);
            // 静态化页面内容
            return FreeMarkerTemplateUtils.processTemplateIntoString(template, params);
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
            throw new Exception(e);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值