SpringBoot基于freemarker根据ftl文件生成html

 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();
        }
    }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值