1.加入Maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2.ftl模板样例
<html>
<head>
<title>freemarker测试</title>
</head>
<body>
<h1>${message},${name}</h1>
</body>
</html>
3.Java代码
try{
//1、创建Configuration对象,指定编码集和模板文件夹
Configuration configuration = new Configuration(Configuration.getVersion());
configuration.setDefaultEncoding("utf-8");
configuration.setDirectoryForTemplateLoading(new File("/Users/admin/web/test/ftl"));
//2、创建Template对象,指定模板文件
Template template = configuration.getTemplate("test.ftl");
//3、准备数据,Map或POJO类型,推荐Map
Map<String,Object> data = new HashMap<>();
data.put("message", "第一个Freemarker例子");
data.put("name", "jmm");
//4、创建Writer对象,指定输出文件
Writer writer = new FileWriter("/Users/admin/test.html");
//5、生成模板
template.process(data,writer);
}catch(Exception e){
e.printStackTrace();
}