1、导入依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2、获取freemarker的模板内容
Map<String, Object> map:存放赋值
String freemarkerName:模板名
模板格式:
${address}
<#list userList as user>
${user.name}
</#list>
public String getTemplateValue(Map<String, Object> map, String freemarkerName) {
String freemarkerContent = "";
try {
Configuration configuration = new Configuration(new Version("2.3.0"));
configuration.setDefaultEncoding("utf-8");
//.ftl配置文件所在路径
String absolutePath = getAbsolutePath();
configuration.setDirectoryForTemplateLoading(new File(absolutePath));
//以utf-8的编码读取ftl模板文件
Template template = configuration.getTemplate(freemarkerName, "utf-8");
//获取freemarker生成的内容
freemarkerContent = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
} catch (Exception e) {
e.printStackTrace();
}
return freemarkerContent;
}