代码:
@SpringBootTest
@RunWith(SpringRunner.class)
public class FreemarkerTest {
@Test
public void testGenerateHtml() throws IOException, TemplateException {
Configuration configuration=new Configuration(Configuration.getVersion());
String classpath = this.getClass().getResource("/").getPath();
configuration.setDirectoryForTemplateLoading(new File(classpath + "/templates/"));
configuration.setDefaultEncoding("utf-8");
Template template = configuration.getTemplate("test1.ftl");
Map map = getMap();
String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
System.out.println(content);
InputStream inputStream = IOUtils.toInputStream(content);
FileOutputStream fileOutputStream = new FileOutputStream(new File("d:/test1.html"));
int copy = IOUtils.copy(inputStream, fileOutputStream);
}
@Test
public void testGenerateHtmlByString() throws IOException, TemplateException {
Configuration configuration=new Configuration(Configuration.getVersion());
String templateString="" +
"<html>\n" +
" <head></head>\n" +
" <body>\n" +
" 名称:${name}\n" +
" </body>\n" +
"</html>";
StringTemplateLoader stringTemplateLoader = new StringTemplateLoader();
stringTemplateLoader.putTemplate("template",templateString);
configuration.setTemplateLoader(stringTemplateLoader);
Template template = configuration.getTemplate("template","utf-8");
Map map = getMap();
String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
System.out.println(content);
InputStream inputStream = IOUtils.toInputStream(content);
FileOutputStream fileOutputStream = new FileOutputStream(new File("d:/test1.html"));
IOUtils.copy(inputStream, fileOutputStream);
}
private Map getMap(){
Map<String, Object> map = new HashMap<>();
map.put("name","三年二班");
Student stu1 = new Student();
stu1.setName("小明");
stu1.setAge(18);
stu1.setMoney(1000.86f);
stu1.setBirthday(new Date());
Student stu2 = new Student();
stu2.setName("小红");
stu2.setMoney(200.1f);
stu2.setAge(19);
List<Student> friends = new ArrayList<>();
friends.add(stu1);
stu2.setFriends(friends);
stu2.setBestFriend(stu1);
List<Student> stus = new ArrayList<>();
stus.add(stu1);
stus.add(stu2);
map.put("stus",stus);
HashMap<String,Student> stuMap = new HashMap<>();
stuMap.put("stu1",stu1);
stuMap.put("stu2",stu2);
map.put("stu1",stu1);
map.put("stuMap",stuMap);
return map;
}
}
模板文件:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf‐8">
<title>Hello World!</title>
</head>
<body>
Hello ${name}!
<br/>遍历数据模型中的stuMap(map数据),第一种方法:在中括号中填写map的key,第二种方法:在map后边直接加"点key"<br/>
姓名:${stuMap['stu1'].name}<br/>
年龄:${stuMap['stu1'].age}<br/>
输出stu1的学生信息:<br/>
姓名:${stuMap.stu1.name}<br/>
年龄:${stuMap.stu1.age}<br/>
遍历map中的key stuMap?keys 就是key列表(是一个list)
遍历输出两个学生信息:<br/>
<table>
<tr>
<td>序号</td>
<td>姓名</td>
<td>年龄</td>
<td>钱包</td>
</tr>
<#list stuMap?keys as k>
<tr>
<#--_index:得到循环的下标,使用方法是在stu后边加"_index",它的值是从0开始-->
<td>${k_index + 1}</td>
<td <#if stuMap[k].name =='小明'>style="background: cornflowerblue"</#if> >${stuMap[k].name}</td>
<td>${stuMap[k].age}</td>
<td >${stuMap[k].money}</td>
</tr>
</#list>
</table>
</body>
</html>