频道栏目
首页 > 资讯 > 其他综合 > 正文

Freemarker 浅析

16-07-25        来源:[db:作者]  
收藏   我要投稿

依赖

我们需要导入一个jar包,名为freemarker.jar。随便到网上下载一个就行,而且对其他诸如servlet等没有依赖,所以我们可以很轻松的进行移植操作。

工具类FreemarkerUtil.java

package main;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class FreemarkerUtil {

    /**
     * 根据给定的ftl(freemarker template language)来获得一个用于操作的模板
     * @param name
     * @return
     */
    public Template getTemplate(String name) {
        try {
            // 通过Freemark而的Configuration读取到相应的模板ftl
            Configuration cfg = new Configuration();
            // 设定去哪里读取相关的模板FTL文件
            cfg.setClassForTemplateLoading(this.getClass(), "/ftl");
            // 在模板文件目录中找到名为name的文件
            Template template = cfg.getTemplate(name);
            return template != null ? template : null;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 通过控制台输出文件信息
     * 
     * @param name
     * @param root
     */
    public void print(String name, Map root) {
        try {
            // 通过Template可以将模板文件输出到相应的流
            Template template = this.getTemplate(name);
            template.process(root, new PrintWriter(System.out));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 输出为HTML文件
     * 
     * @param name
     * @param root
     * @param outfile
     */
    public void htmlprint(String name, Map root, String outfile) {
        FileWriter writer = null;
        try {
            // 使用一个路径实现将文件的输出
            writer = new FileWriter(new File("src/"+ outfile));
            Template template = this.getTemplate(name);
            template.process(root, writer);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

案例分析一

测试代码如下:

/**
     * 仅仅针对有一个数据的测试
     * 
     * @throws Exception
     */
    @Test
    public void testftl1() throws Exception {
        FreemarkerUtil util = new FreemarkerUtil();
        Template template = util.getTemplate("01.ftl");
        Map map = new HashMap();
        map.put("username", "XIAO MARK");
        // 默认输出到了控制台上
        template.process(map, new OutputStreamWriter(System.out));
    }

案例分析二

package main;

import java.io.Serializable;

public class User implements Serializable {

    private int id;
    private String name;
    private int age;
    private Group group;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", age=" + age + ", group=" + group + "]";
    }

}

内有组合类Group的使用,

package main;

import java.io.Serializable;

public class Group implements Serializable{

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Group [name=" + name + "]";
    }

}

测试代码如下:

/**
     * 控制台输出带有对象的模板使用案例
     * 
     * @throws Exception
     */
    @Test
    public void testftl3() throws Exception {
        FreemarkerUtil util = new FreemarkerUtil();
        Template template = util.getTemplate("03.ftl");
        Map map = new HashMap();
        User user = new User();
        user.setId(1);
        user.setName(" 妈的智障 ");
        user.setAge(21);
        map.put("user", user);
        template.process(map, new OutputStreamWriter(System.out));
    }
/**
     * 输出HTML文件形式的带有对象的测试案例
     * 
     * @throws Exception
     */
    @Test
    public void testftl3outtofile() throws Exception {
        FreemarkerUtil util = new FreemarkerUtil();
        Template template = util.getTemplate("03.ftl");
        Map map = new HashMap();
        User user = new User();
        user.setId(1);
        user.setName(" 妈的智障 ");
        user.setAge(21);
        map.put("user", user);
        util.htmlprint("03.ftl", map, "./../page/03ftloutfile.html");
    }

案例分析三

测试代码如下:

/**
     * 输出HTML文件形式的带有对象的测试案例
     * 
     * @throws Exception
     */
    @Test
    public void testftl5outtofile() throws Exception {
        FreemarkerUtil util = new FreemarkerUtil();
        Template template = util.getTemplate("03.ftl");
        Map map = new HashMap();
        List users = new ArrayList();
        for (int i = 1; i <= 10; i++) {
            User user = new User();
            user.setId(i);
            user.setName(" 妈的智障 " + (i * i));
            user.setAge((int) (Math.random() * 100));
            users.add(user);
        }
        map.put("users", users);
        util.htmlprint("05.ftl", map, "./../page/05ftloutfile.html");
    }

案例分析四

测试代码如下:

@Test
    public void testftl6() throws Exception {
        FreemarkerUtil util = new FreemarkerUtil();
        Map map = new HashMap();
        User user = new User();
        Group group = new Group();
        group.setName("1234");
        user.setId(28);
        user.setAge(27);
        user.setName("  妈的智障      ");
        user.setGroup(group);
        map.put("user", user);
        util.print("06.ftl", map);
    }

程序运行结果浏览

项目目录
集合形式对象内容遍历
相关TAG标签
上一篇:JS最佳实践
下一篇:React重构:移动web极致优化
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站