Freemarker:初识(搭建环境、对象、集合、索引、赋值、null、时间、宏定义)

1、Freemarker环境搭建

(1)导入依赖


      org.freemarker
      freemarker
      2.3.16
    

(2)创建freemarker.html


"en">

    "UTF-8">
    Title


${zhai}

(3)创建测试类

public class FMDemo {
    public static void main(String[] args) throws IOException, TemplateException {
        //配置对象
        Configuration configuration=new Configuration();
        //模板路径
        String dir="D:\\IdeaProjects\\Freemarker_01\\ftl\\";
        //导入模板目录
        configuration.setDirectoryForTemplateLoading(new File(dir));
        //获取模板
        Template template=configuration.getTemplate("freemarker.html");
        //数据
        Map root=new HashMap();
        root.put("zhai","hello");
        Writer out=new FileWriter(new File(dir)+"\\hello.html");
        //生成开始
        template.process(root,out);
        //关闭资源
        out.close();
    }
}

(4)测试

Freemarker:初识(搭建环境、对象、集合、索引、赋值、null、时间、宏定义)_第1张图片

 

 

 (5)项目结构

Freemarker:初识(搭建环境、对象、集合、索引、赋值、null、时间、宏定义)_第2张图片

 

 

 

2、对象

(1)测试类

public class FMDemo {
    public static void main(String[] args) throws IOException, TemplateException {
        //配置对象
        Configuration configuration=new Configuration();
        //模板路径
        String dir="D:\\IdeaProjects\\Freemarker_01\\ftl\\";
        //导入模板目录
        configuration.setDirectoryForTemplateLoading(new File(dir));
        //获取模板
        Template template=configuration.getTemplate("freemarker.html");
        //数据
        Map root=new HashMap();
        Student student=new Student();
        student.setSex("");
        student.setSname("zhai");
        root.put("student",student);
        Writer out=new FileWriter(new File(dir)+"\\hello.html");
        //生成开始
        template.process(root,out);
        //关闭资源
        out.close();
    }
}

(2)freemarker.html

${student.sex}/////${student.sname}

(3)创建学生类

public class Student {
    private String sname;
    private String sex;
    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

}

(4)测试

Freemarker:初识(搭建环境、对象、集合、索引、赋值、null、时间、宏定义)_第3张图片

 

 

 

3、List集合

(1)测试类

public class FMDemo {
    public static void main(String[] args) throws IOException, TemplateException {
        //配置对象
        Configuration configuration=new Configuration();
        //模板路径
        String dir="D:\\IdeaProjects\\Freemarker_01\\ftl\\";
        //导入模板目录
        configuration.setDirectoryForTemplateLoading(new File(dir));
        //获取模板
        Template template=configuration.getTemplate("freemarker.html");
        //数据
        Map root=new HashMap();
        List students =new ArrayList();
        students.add("zhang");
        students.add("zhai");
        students.add("ma");
        root.put("students",students);
        Writer out=new FileWriter(new File(dir)+"\\hello.html");
        //生成开始
        template.process(root,out);
        //关闭资源
        out.close();
    }
}

(2)freemarker.html


<#list students as student>
   ${student}

(3)测试

Freemarker:初识(搭建环境、对象、集合、索引、赋值、null、时间、宏定义)_第4张图片

 

 

 

4、Map集合

(1)创建Map集合

 Map root=new HashMap();
        Map map=new HashMap();
        map.put("z","zhai");
        map.put("l","liu");
        root.put("map",map);

(2)freemarker.html


<#list map?keys as key>
   ${map[key]}

(3)测试

Freemarker:初识(搭建环境、对象、集合、索引、赋值、null、时间、宏定义)_第5张图片

 

 

 

5、List_map集合

(1)测试类

 //获取模板
        Template template=configuration.getTemplate("freemarker.html");
        //数据
        Map root=new HashMap();
        List maps=new ArrayList();
        Map stu1=new HashMap();
        stu1.put("num1","zhai");
        stu1.put("num2","zhang");
        Map stu2=new HashMap();
        stu1.put("num3","ma");
        stu1.put("num4","zhao");
        maps.add(stu1);
        maps.add(stu2);
        root.put("maps",maps);
        Writer out=new FileWriter(new File(dir)+"\\hello.html");

(2)freemarker.html

<#list maps as map>
    <#list map?keys as key>
        ${map[key]}
    

(3)测试

Freemarker:初识(搭建环境、对象、集合、索引、赋值、null、时间、宏定义)_第6张图片

 

 

 

6、索引

(1)测试类

 Map root=new HashMap();
        List students =new ArrayList();
        students.add("zhang");
        students.add("zhai");
        students.add("ma");
        root.put("students",students);
        Writer out=new FileWriter(new File(dir)+"\\hello.html");

(2)freemarker.html


<#list students as student>
    ${student_index}

(3)测试

Freemarker:初识(搭建环境、对象、集合、索引、赋值、null、时间、宏定义)_第7张图片

 

 

7、在模板中进行赋值

(1)取标签数据


<#assign x=0/>
${x}

 

 (2)取后台数据


<#assign x='${zhai}'/>
${x}

 

 (3)集合


<#assign x>
<#list ["a","b","c"] as n>${n}

${x}

Freemarker:初识(搭建环境、对象、集合、索引、赋值、null、时间、宏定义)_第8张图片

 

 (4)条件

<#assign x>
<#list ["a","b","c"] as n>
<#if n!="a">
${n}
#if>


${x}

Freemarker:初识(搭建环境、对象、集合、索引、赋值、null、时间、宏定义)_第9张图片

 

 

8、时间

(1)time格式


${time?time}

 

 

 

 (2)datetime格式


${time?datetime}

 

 

9、null的处理


${zhai!"我是null"}

 

 如果不去声明null的处理的话会报错

 

10、宏定义


<#macro table pageNo>
${pageNo}

<@table pageNo=8/>

 

你可能感兴趣的:(Freemarker:初识(搭建环境、对象、集合、索引、赋值、null、时间、宏定义))