FTL


一. 什么是FTL

FTLFreeMarker,是一款模板引擎,类似于Thymeleaf ,都是通过传入的数据来对页面进行渲染,用来生成输出文本(HTML网页、电子邮件、配置文件、源代码等)的通用工具。

二. 常见语法讲解

2.1 普通填值


利用${}来进行内容填充,将尖括号里面的变量替换为实际值

<lxd_name>${lxd_name}</lxd_name>

2.2 if 判断


利用ifelseif来进行条件的判断

<#if (source<60)>
    <p>不及格</p>
<#elseif (source>=60)>
    <p>及格</p>
</#if>

2.3 list遍历


如果我们传递一个interfaceFtlList集合,那么将在模板中遍历渲染

<#list interfaceFtlList as interfaceFtl>
    <interface>
        <name>${interfaceFtl.name}</name>
        <ipAddress>${interfaceFtl.ipAddress}</ipAddress>
        <subnetMask>${interfaceFtl.subnetMask}</subnetMask>
    </interface>
</#list>

2.4 判断非空


有的时候我们插入的值可能为空或者根本就没有相关属性,那么我们就需要判断非空再来填入数据

<#if ospfRedis??>
    <ospf_redistribute>${ospfRedis}</ospf_redistribute>
</#if>

三. 项目整合

3.1 导入依赖


<!--ftl生成xml-->
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.31</version>
</dependency>

3.2 编写模板文件


根据上节讲解的内容编写相关的模板,例如

<Information>
    <type>host</type>
    <lxd_name>${lxd_name}</lxd_name>
    <#if interfaceFtlList??>
    <interfaces>
        <#list interfaceFtlList as interfaceFtl>
        <interface>
            <name>${interfaceFtl.name}</name>
            <ipAddress>${interfaceFtl.ipAddress}</ipAddress>
            <subnetMask>${interfaceFtl.subnetMask}</subnetMask>
        </interface>
        </#list>
    </interfaces>
    </#if>
</Information>

3.3 使用


先实例化方法

public class FtlXml {
	/**
	     * @description  将传入的Map键值对转化为xml文件
	     * @params [dataMap:传入的键值对集合, type:容器类型【 Router或 Host】, lxdName:容器名称, savePath:生成的xml存放路径]
	     * @returns void
	     */
	public void ftlToXml(Map<String, Object> dataMap, String type,String lxdName,String savePath) throws IOException, TemplateException {
	    //设置版本
	    Configuration configuration = new Configuration(Configuration.VERSION_2_3_31);
	    //加载ftl模板所在的父级目录
	    configuration.setDirectoryForTemplateLoading(new File("src/main/java/com/nuaa/automaticallygeneratenetwork/protocolXml/createXML/ftlExample"));
	    configuration.setDefaultEncoding("utf-8");
	    Template template = null;
	    //选择相关ftl配置文件
	    if ("Router".equals(type)){
	        //父级目录下ftl模板的名称
	        template = configuration.getTemplate("RouterExample.ftl", "utf-8");
	    }else {
	        //父级目录下ftl模板的名称
	        template = configuration.getTemplate("HostExample.ftl", "utf-8");
	    }
	    //创建一个位置用于存放生成的文件,指定生成xml文件
	    Writer writer = new FileWriter(new File(savePath+"/"+lxdName+".xml"));
	    template.process(dataMap,writer);
	}
}

传入相关参数进行测试

public static void main(String[] args) throws TemplateException, IOException {
    Map<String, Object> dataMap = new HashMap<>();

    dataMap.put("lxd_name","QH2");

    List<InterfaceFtl> interfaceFtlList = new ArrayList<>();
    interfaceFtlList.add(new InterfaceFtl("eth1","192.168.1.1","24"));
    interfaceFtlList.add(new InterfaceFtl("eth2","192.168.2.1","24"));
    dataMap.put("interfaceFtlList",interfaceFtlList);

    FtlXml ftlXml = new FtlXml();
    ftlXml.ftlToXml(dataMap,"Host","QH2","src/main/java/com");

}

生成文件如下
在这里插入图片描述

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐