freemarker依赖
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.ftl</include>
</includes>
<!-- activate spring profile -->
<filtering>true</filtering>
</resource>
import freemarker.template.Configuration; import freemarker.template.DefaultObjectWrapper; import freemarker.template.Template; import freemarker.template.TemplateException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.StringWriter; import java.io.Writer; import java.util.Locale; import java.util.Map; public class FreeMarkerUtil { private final static Logger logger = LoggerFactory.getLogger(FreeMarkerUtil.class); private static Configuration conf = null; static { conf = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
conf.setDirectoryForTemplateLoading(
new File(Thread.currentThread().getContextClassLoader().getResource("flt").getPath()));
conf.setObjectWrapper(new DefaultObjectWrapper(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS)); conf.setLocale(Locale.CHINA); conf.setDefaultEncoding("utf-8"); conf.setClassicCompatible(true); } public static String getString(Map<String,Object> root,String tplName) { Writer out = new StringWriter(2048); try { Template temp = conf.getTemplate(tplName); temp.process(root, out); } catch (IOException e) { logger.error(e.getMessage(),e); } catch (TemplateException e) { logger.error(e.getMessage(),e); } return out.toString(); } }
<html>
<head>
<title>xx统计</title>
</head>
<body>
<#list list as item>
${item.title}:<br/>
<table border="1">
<tr>
<#list item.headList as head>
<th>${head}</th>
</#list>
</tr>
<#list item.dataList as data>
<tr>
<#list data as d>
<td>${d}</td>
</#list>
</tr>
</#list>
</table>
<br/>
</#list>
</body>
</html>