输出Freemarker模板

来源:互联网 发布:php判断域名是否拦截 编辑:程序博客网 时间:2024/05/29 21:37

用Freemarker模板输出文件

建立Freemarker模板

freemarker.ftl

<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Welcome!</title></head><body>    <#if username == "李雨翔">        <h1>            Welcome 大帅哥        </h1>    <#else>        <h1>            Welcome ${username}        </h1>    </#if>    <ul>        <#list goodsList as goods>             <li><span>Good's name: </span><a href="${goods.url}">${goods.name}</a></li>        </#list>    </ul></body></html>

输出freemarker的java程序

Goods.java

package com.lyx.freemaker;public class Goods {    private String name;    private String url;    public Goods() {    }    public Goods(String name, String url) {        super();        this.name = name;        this.url = url;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getUrl() {        return url;    }    public void setUrl(String url) {        this.url = url;    }}

Freemarker.java

package com.lyx.freemaker;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;import java.io.Writer;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import freemarker.template.Configuration;import freemarker.template.Template;import freemarker.template.TemplateException;import freemarker.template.Version;public class Freemaker {    public static void main(String[] args) throws IOException, TemplateException {        Version version = new Version("2.3.23");        Configuration cfg = new Configuration(version);        //扫描路径        cfg.setDirectoryForTemplateLoading(new File("D:\\eclipse-workspace\\freemarker\\src\\main\\java"));        //扫描的文件(模板)        Template template = cfg.getTemplate("freemaker.ftl","utf-8");        //即便模板上的占位符取不到值也不报错,做静默处理(不取值-空字符串)        template.setClassicCompatible(true);        //输出的目标文件        Writer writer = new OutputStreamWriter(new FileOutputStream("D:\\BaiduNetdiskDownload\\freemaker.html"), "utf-8");        //输出到控制台//      Writer writer = new OutputStreamWriter(System.out);        //添加数据        Map<String, Object> dataModel = new HashMap<>();        dataModel.put("username", "李雨翔");        Goods goods = new Goods("Head first", "https://detail.tmall.com/item.htm?spm=a220m.1000858.1000725.1.7e43e67a2zjSoD&id=526232391362&areaId=510700&user_id=327369967&cat_id=2&is_b=1&rn=8bf7381d3a156942e22689f842dba043");        Goods goods1 = new Goods("PHP", "https://detail.tmall.com/item.htm?spm=a220o.1000855.0.0.39c8b885YVHho2&abtest=_AB-LR67-PR67&pvid=969ca72c-1992-469f-b912-bdcf5f571786&pos=1&abbucket=_AB-M67_B14&acm=03067.1003.1.1977615&id=526497761968&scm=1007.12776.82642.100200300000000");        Goods goods2 = new Goods("java入门", "https://detail.tmall.com/item.htm?spm=a220m.1000858.1000725.1.16a9dc7aIyxLbf&id=559132036057&skuId=3649437991258&areaId=510700&user_id=2646031546&cat_id=2&is_b=1&rn=6ddf4ec2b2ea8f07c0f689ddba0fdb92");        List<Goods> goodsList= new ArrayList<>();        goodsList.add(goods);        goodsList.add(goods1);        goodsList.add(goods2);        dataModel.put("goodsList", goodsList);        //将数据和模板糅合在一起输出        template.process(dataModel, writer);        writer.close();    }}
原创粉丝点击