美文网首页
7.html占位符替换

7.html占位符替换

作者: _少年不知愁 | 来源:发表于2024-09-18 14:10 被阅读0次

1.技术freemarker

代码 https://gitee.com/J-summit/note-sty-blogs/tree/master/src/main/java/tech/cn/note/html

image.png
参考https://www.cnblogs.com/itdragon/p/7750903.html
http://freemarker.foofun.cn/index.html

2.代码实现

@Service
public class FreeMakerDataFillServiceImpl implements FreeMakerDataFillService {

    private final List<DocumentConvert> convertList;

    @Autowired
    public FreeMakerDataFillServiceImpl(List<DocumentConvert> convertList) {
        this.convertList = convertList;
    }

    @Override
    public byte[] processToByte(File templateFile, Map<String, Object> dataSource) {
        return doProcess(templateFile.getParent(), templateFile.getName(), dataSource);
    }

    @Override
    public byte[] processToByte(String templatePath, Map<String, Object> dataSource) {
        File templateFile = checkTemplateFile(templatePath);
        return doProcess(templateFile.getParent(), templateFile.getName(), dataSource);
    }

    @Override
    public byte[] processToByte(String templateDir, String templateName, Map<String, Object> dataSource) {
        checkTemplateFile(templateDir, templateName);
        return doProcess(templateDir, templateName, dataSource);
    }

    @Override
    public ByteArrayInputStream processToStream(File templateFile, Map<String, Object> dataSource) {
        byte[] outBytes = processToByte(templateFile, dataSource);
        if (outBytes == null || outBytes.length < 1) {return null;}
        //转换为字节流
        return new ByteArrayInputStream(outBytes);
    }

    @Override
    public ByteArrayInputStream processToStream(String templatePath, Map<String, Object> dataSource) {
        byte[] outBytes = processToByte(templatePath, dataSource);
        if (outBytes == null || outBytes.length < 1){ return null;}
        //转换为字节流
        return new ByteArrayInputStream(outBytes);
    }

    @Override
    public ByteArrayInputStream processToStream(String templateDir, String templateName, Map<String, Object> dataSource) {
        byte[] outBytes = processToByte(templateDir, templateName, dataSource);
        if (outBytes == null || outBytes.length < 1) {return null;}
        //转换为字节流
        return new ByteArrayInputStream(outBytes);
    }


    /**
     * 填充数据至模板中
     */
    private byte[] doProcess(String templateDir, String templateName, Map<String, Object> dataSource) {
        DocumentConfiguration fillConfig;
        StringWriter outWriter;
        try {
            log.debug("模板名称:{},填充数据开始,data is {}", templateName, JSONUtil.parse(dataSource));
            //填充数据转换自定义方法
            if (CollUtil.isNotEmpty(convertList)) {
                convertList.forEach(c -> dataSource.put(c.getConvertName(), c));
            }
            //根据模板目录获取对象
            fillConfig = new DocumentConfiguration(templateDir);
            outWriter = new StringWriter();
            //获取模板对象
            Template template = fillConfig.getTemplate(templateName);
            //填充数据至模板中
            template.process(dataSource, outWriter);
            log.info("模板名称:{},填充数据结束...", templateName);
            return outWriter.toString().getBytes(StandardCharsets.UTF_8);
        } catch (Exception e) {
            String msg = String.format("填充模板数据出错, 模板名称:%1s", templateName);

            log.error(msg + ", 出错信息: " + e);
            throw new RuntimeException(msg);
        }
    }


    /**
     * 检查模板路径
     */
    private File checkTemplateFile(String templatePath) {
        if (StringUtils.isBlank(templatePath)) {
            throw new RuntimeException("数据填充模板路径不允许为空");
        }
        File templateFile = new File(templatePath);
        if (!templateFile.exists()) {
            throw new RuntimeException("数据填充模板文件不存在");
        }
        return templateFile;
    }

    /**
     * 检查模板路径
     */
    private File checkTemplateFile(String templateDir, String templateName) {
        if (StringUtils.isBlank(templateDir)) {
            throw new RuntimeException("数据填充模板目录不允许为空");
        }
        if (StringUtils.isBlank(templateName)) {
            throw new RuntimeException("数据填充模板名称不允许为空");
        }
        String templatePath = String.format("%1s%2s%3s", templateDir, File.separator, templateName);
        File templateFile = new File(templatePath);
        if (!templateFile.exists()) {
            throw new RuntimeException("数据填充模板文件不存在");
        }
        return templateFile;
    }
}

相关文章

  • Mybatis

    Mybatis 常见 #{} 和 ${} {}:sql占位符 ${}:字符替换,properties中的变量占位符...

  • 占位符替换

  • 占位符替换

  • JAVA 代码调用Jmeter (动态配置请求参数)

    一、Jmeter如何进行动态设置请求参数 Jmeter使用占位符的方式进行动态替换请求参数内容。 那么替换占位符的...

  • JAVA操作Word合并、替换占位符、Word插入富文本、生成水

    文章概览 引入POI类库及注意事项 多个Word文档合并 替换文档中的占位符,包含段落占位符、表格占位符 富文本插...

  • 请你实现一个简单的字符串替换函数(python)

    例:请你实现一个简单的字符串替换函数。原串中需要替换的占位符为"%s",请按照参数列表的顺序一一替换占位符。若参数...

  • mybatis $#符号的区别

    ${ }:字符串替换 { }:占位符 ${ }在动态 SQL 解析阶段会直接进行变量替换,而#{ }会被解析成占位...

  • 莹莹

    占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符...

  • 莹莹

    占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符占位符...

  • String方法format

    //1。%s , %d ,%.2f %c称为占位符 //2.这些占位符由后面变量来替换 //3.%s 表示后面由字...

网友评论

      本文标题:7.html占位符替换

      本文链接:https://www.haomeiwen.com/subject/qtmnljtx.html