Total Pageviews

2020/01/10

[Java] [Freemarker] Generate Html file from FTL?

Requirement
I would like to generate HTML file as bellows:
<html>
<body>
    <p>Hello Albert! You have the following messages:
        <li><b>Tim:</b> Please don't forget to bring the laptop!</li>
        <li><b>Cindy:</b> Can you give me a visit this afternoon?</li>
        <li><b>Richard:</b> Don't forget the papers this time!</li>
        <li><b>Mom:</b> Don't forget the milk this time!</li>
        </p>
</body>
</html>

FTL template file
<html>
<body>
    <p>Hello ${name}! You have the following messages:
    <#if messages??>
    <#-- if message has data, get data via iteration
         https://freemarker.apache.org/docs/ref_directive_if.html -->
        <#list messages as m>
        <li><b>${m.from}:</b> ${m.body}</li>
        </#list>
    <#else> <#-- if message is null, then show NO Message Found -->
        No Message Found!
    </#if>
    </p>
</body>
</html>


Test Case
package com.test.batch;

import com.test.batch.exception.FtlException;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.Builder;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class FtlTest {

    private Configuration cfg;

    @Before
    public void init() {
        cfg = new Configuration();
        // 設定到 classpath 讀取 ftl file
        cfg.setClassForTemplateLoading(this.getClass(), "/");
    }

    @Test
    public void testMessageHtml() {
        try (Writer file = new FileWriter(new File("C:/ftl_message.html"));) {
            Template template = cfg.getTemplate("ftl/message.ftl");

            Map<String, Object> data = new HashMap<>();
            data.put("name", "Albert");

            List<Message> messages = new ArrayList<>();
            messages.add(Message.builder().from("Tim").body("Please don't forget to bring the laptop!").build());
            messages.add(Message.builder().from("Cindy").body("Can you give me a visit this afternoon?").build());
            messages.add(Message.builder().from("Richard").body("Don't forget the papers this time!").build());
            messages.add(Message.builder().from("Mom").body("Don't forget the milk this time!").build());

            data.put("messages", messages);

            template.process(data, file);
        } catch (IOException | TemplateException e) {
            throw new FtlException("fail to generate file from ftl file : " + e.getMessage(), e);
        }
    }

    @Getter
    @Builder
    public static class Message {
        private String from;
        private String body;
    }

}




No comments: