zoukankan      html  css  js  c++  java
  • freemarker使用map替换ftl中相关值

    ftl文件demo01.ftl

    <html>
    <head>
    <title>Welcome!</title>
    </head>
    <body>
    <h1>Welcome ${user}!</h1>
    <p>Our latest product:
    <a href="${url}">${name}</a>!
    </body>
    </html>

    java类  Demo01.java

    package demo01;

    import java.io.File;
    import java.io.OutputStreamWriter;
    import java.util.HashMap;
    import java.util.Map;

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

    public class Demo01 {

    public static void main(String[] args) throws Exception {

    Configuration cfg = new Configuration();
    File file = new File(Demo01.class.getResource("/").getPath().substring(1)+"template");
    System.out.println(file.getPath());

    //设置模板文件所在目录
    cfg.setDirectoryForTemplateLoading(file);

     //构造填充数据的Map 

    Map map = new HashMap();
    map.put("user", "lavasoft");
    map.put("url", "http://www.baidu.com/");
    map.put("name", "crd");

    //设置模板文件名
    Template tmp = cfg.getTemplate("demo01.ftl");
    tmp.process(map, new OutputStreamWriter(System.out));


    }

    }

    目录结构

     执行后台打印:

     所需jar包

  • 相关阅读:
    升级centos6.5系统的gcc为4.8.5的简易步骤
    赛车比赛(洛谷U4566)
    月考(cogs 1176)
    xth的旅行(codevs 1450)
    魔法禁书目录2:回家(codevs 3024)
    交换
    牛的旅行(洛谷 1522)
    长途旅行
    序列问题
    正确答案
  • 原文地址:https://www.cnblogs.com/rdchen/p/11082812.html
Copyright © 2011-2022 走看看