test.ftl
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
table {
border-top: 1px solid #333;
border-left: 1px solid #333;
border-spacing: 0;
background-color: #fff;
width: 100%
}
table td {
border-bottom: 1px solid #333;
border-right: 1px solid #333;
font-size: 13px;
padding: 5px
}
.et2 {
text-align: center;
}
.font0 {
color: rgb(0, 0, 0);
}
</style>
</head>
<body>
<table style="width:271.82pt">
<colgroup>
<col width="193" style="width:115.80pt;">
<col width="260" style="width:156.00pt;">
</colgroup>
<tbody>
<tr height="34">
<td colspan="2" class="et2">标题</td>
</tr>
<tr height="34">
<td class>姓名</td>
<td class>${header!}</td>
</tr>
<#list aaa as item>
<tr height="23">
<td class>${item.name!''}</td>
<td class>${item.age!}</td>
</tr>
</#list>
</tbody>
</table>
</body>
</html>
实体类
@Data
public class User66 {
private String name;
private String age;
}
测试方法
public class TestHtml {
public static void main(String[] args) {
User66 user1 = new User66();
user1.setName("张三");
user1.setAge("18");
List<User66> users = new ArrayList<>();
users.add(user1);
users.add(user1);
users.add(user1);
users.add(user1);
users.add(user1);
try {
freemarker.template.Configuration configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
configuration.setClassForTemplateLoading(TestHtml.class, "/");
Template template = configuration.getTemplate("templates/test.ftl");
Map<String, Object> data = new HashMap<>();
data.put("header", "Welcome to my website!");
System.out.println(users.toString());
data.put("aaa", users);
StringWriter out = new StringWriter();
template.process(data, out);
String html = out.toString();
System.out.println(html);
} catch (IOException | TemplateException e) {
e.printStackTrace();
}
}