一、导入依赖

        <dependency>
            <groupId>freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.9</version>
        </dependency>

二、编写FreeMarker单元测试

    @Test
    public void FreeMarkerDemo() throws IOException, TemplateException {
        //1.创建FreeMarker配置对象
        Configuration configuration = new Configuration();
        //2.指定模板文件路径
        configuration.setDirectoryForTemplateLoading(Path.of("D:\\Test").toFile());
        //3.准备数据模型
        HashMap<String ,Object> dataModel = new HashMap<>();
        dataModel.put("user","john");
        dataModel.put("employees",new ArrayList<employee>()
        {{add(new employee("lucy"));add(new employee("tom"));}});
        //4.加载模板
        Template template = configuration.getTemplate("freemarkerTemplateDemo.ftl");
        //5.构造输出对象并输出文本 System.out输出到控制台 FileWriter输出到文件
        Writer console = new OutputStreamWriter(System.out);
        FileWriter fileWriter = new FileWriter(Paths.get("D:\\Test\\result.html").toFile());
        template.process(dataModel, console);template.process(dataModel, fileWriter);
        console.flush();fileWriter.flush();
    }

使用的ftl模板如下

<html>
    <head>
        <title>Hello FreeMarker</title>
    </head> 
    <body>
    <#-- 这是注释在最后的输出总并不会被输出 --> 
    <#-- 下面使用插值${user}最终输出时会被替换 --> 
    <h1>Welcome ${user} !</h1> 
    <u1>
    <#-- FTL指令 与3大前端框架的指令类似 可对比类似理解 --> 
    <#list employees as employee> 
        <li>${employee.name}<br> 
    </#list>
    <u1>
    </body> 
</html>

 三、Springboot集成FreeMarker

1.导入依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
            <version>2.6.6</version>
        </dependency>

2.yaml文件配置

spring:
  freemarker:
    template-loader-path: classpath:templates #指定模板文件放置路径
    charset: UTF-8 #指定编码

3.Springboot集成FreeMarker单元测试如下

@SpringBootTest(classes = Application.class)
public class FreeMarkerSpringBootTest {
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Test
    void FreeMarkerSpringBootTestDemo() {
        try {
            //1.准备数据模型
            HashMap<String ,Object> dataModel = new HashMap<>();
            dataModel.put("user","john");
            dataModel.put("employees",new ArrayList<employee>()
            {{add(new employee("lucy"));add(new employee("tom"));}});
            //2.获取模板
            Template template = freeMarkerConfigurer.getConfiguration().getTemplate("demo.ftl");
            //处理成字符串
            String output = FreeMarkerTemplateUtils.processTemplateIntoString(template, dataModel);
            System.out.println(output);
            //转成文件
            Files.writeString(Path.of("D:\\Test\\result.txt"),output);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (TemplateException e) {
            throw new RuntimeException(e);
        }
    }
}

 

posted on 2023-04-05 13:19  霸王的笔尖  阅读(79)  评论(0)    收藏  举报