SpringBoot 整合freemarker且模板存在于数据库使用方式

开始

以前是用到过freemarker模板引擎,都是SSM时代,近期项目有个需求,管理员在界面文板框输入html代码块作为模板,然后后期后台获取数据及模板内容(html代码块)整合生成PDF。具体导出pdf后台用到itextpdf。上篇文章写了整合方式及工具类。

maven信息

SpringBoot添加freemarker

	<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

freemark读取html代码块及数据整合方法

/**
 * 数据,,模板信息(html代码块)
 */
@SuppressWarnings("deprecation")
	public static String generateMain(Map<String, List<xxx>> map, String template)
			throws IOException, TemplateException {
		freemarker.template.Configuration cfg = new freemarker.template.Configuration();
		StringTemplateLoader stringLoader = new StringTemplateLoader();
		stringLoader.putTemplate("myTemplate", template);
		cfg.setTemplateLoader(stringLoader);
		freemarker.template.Template temp = cfg.getTemplate("myTemplate", "utf-8");
		Writer out = new StringWriter(2048);
		temp.process(map, out);
		return out.toString();
	}

由于本次模板直接从数据库获取,所以在application.yml中没添加关于freemarker的任何信息

猜你喜欢

转载自blog.csdn.net/u012817163/article/details/107996982