首页 > 其他 > 详细

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

时间:2020-07-28 17:07:52      阅读:70      评论:0      收藏:0      [点我收藏+]

1、Freemarker环境搭建

(1)导入依赖

<dependency>
      <groupId>org.freemarker</groupId>
      <artifactId>freemarker</artifactId>
      <version>2.3.16</version>
    </dependency>

(2)创建freemarker.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
${zhai}
</body>
</html>

(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)测试

技术分享图片

 

 

 (5)项目结构

技术分享图片

 

 

 

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

<body>${student.sex}/////${student.sname}
</body>

(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)测试

技术分享图片

 

 

 

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

<body>
<#list students as student>
   ${student}
</#list>
</body>

(3)测试

技术分享图片

 

 

 

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

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

(3)测试

技术分享图片

 

 

 

5、List_map集合

(1)测试类

 //获取模板
        Template template=configuration.getTemplate("freemarker.html");
        //数据
        Map root=new HashMap();
        List<Map> maps=new ArrayList<Map>();
        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]}
    </#list>
</#list>

(3)测试

技术分享图片

 

 

 

6、索引

(1)测试类

 Map root=new HashMap();
        List students =new ArrayList<String>();
        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

<body>
<#list students as student>
    ${student_index}
</#list>
</body>

(3)测试

技术分享图片

 

 

7、在模板中进行赋值

(1)取标签数据

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

技术分享图片

 

 (2)取后台数据

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

技术分享图片

 

 (3)集合

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

技术分享图片

 

 (4)条件

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

技术分享图片

 

 

8、时间

(1)time格式

<body>
${time?time}
</body>

技术分享图片

 

 技术分享图片

 

 (2)datetime格式

<body>
${time?datetime}
</body>

技术分享图片

 

 

9、null的处理

<body>
${zhai!"我是null"}
</body>

技术分享图片

 

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

 

10、宏定义

<body>
<#macro table pageNo>
${pageNo}
</#macro>
<@table pageNo=8/>
</body>

技术分享图片

 

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

原文:https://www.cnblogs.com/zhai1997/p/13391603.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!