zoukankan      html  css  js  c++  java
  • Freemarker简单用法

    Freemarker 最简单的例子程序
     
    freemarker-2.3.18.tar.gz
     
     
    1、通过String来创建模版对象,并执行插值处理
     
    import freemarker.template.Template; 

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

    /** 
    * Freemarker最简单的例子 

    * @author leizhimin 11-11-17 上午10:32 
    */
     
    public class Test2 { 
            public static void main(String[] args) throws Exception{ 
                    //创建一个模版对象 
                    Template t = new Template(nullnew StringReader("用户名:${user};URL:    ${url};姓名:  ${name}"), null); 
                    //创建插值的Map 
                    Map map = new HashMap(); 
                    map.put("user""lavasoft"); 
                    map.put("url""http://www.baidu.com/"); 
                    map.put("name""百度"); 
                    //执行插值,并输出到指定的输出流中 
                    t.process(map, new OutputStreamWriter(System.out)); 
            } 
    }
     
    执行后,控制台输出结果:
    用户名:lavasoft;URL:    http://www.baidu.com/;姓名:  百度 
    Process finished with exit code 0
     
     
    2、通过文件来创建模版对象,并执行插值操作
     
    import freemarker.template.Configuration; 
    import freemarker.template.Template; 

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

    /** 
    * Freemarker最简单的例子 

    * @author leizhimin 11-11-14 下午2:44 
    */
     
    public class Test { 
            private Configuration cfg;            //模版配置对象 

            public void init() throws Exception { 
                    //初始化FreeMarker配置 
                    //创建一个Configuration实例 
                    cfg = new Configuration(); 
                    //设置FreeMarker的模版文件夹位置 
                    cfg.setDirectoryForTemplateLoading(new File("G:\testprojects\freemarkertest\src")); 
            } 

            public void process() throws Exception { 
                    //构造填充数据的Map 
                    Map map = new HashMap(); 
                    map.put("user""lavasoft"); 
                    map.put("url""http://www.baidu.com/"); 
                    map.put("name""百度"); 
                    //创建模版对象 
                    Template t = cfg.getTemplate("test.ftl"); 
                    //在模版上执行插值操作,并输出到制定的输出流中 
                    t.process(map, new OutputStreamWriter(System.out)); 
            } 

            public static void main(String[] args) throws Exception { 
                    Test hf = new Test(); 
                    hf.init(); 
                    hf.process(); 
            } 
    }
     
    创建模版文件test.ftl
    <html> 
    <head> 
        <title>Welcome!</title> 
    </head> 
    <body> 
        <h1>Welcome ${user}!</h1> 
        <p>Our latest product: 
        <a href="${url}">${name}</a>
    </body> 
    </html> 

    尊敬的用户你好: 
    用户名:${user}; 
    URL:    ${url}; 
    姓名:  ${name}
     
    执行后,控制台输出结果如下:
    <html> 
    <head> 
        <title>Welcome!</title> 
    </head> 
    <body> 
        <h1>Welcome lavasoft!</h1> 
        <p>Our latest product: 
        <a href="http://www.baidu.com/">百度</a>! 
    </body> 
    </html> 

    尊敬的用户你好: 
    用户名:lavasoft; 
    URL:    http://www.baidu.com/; 
    姓名:  百度 
    Process finished with exit code 0
  • 相关阅读:
    解决网站出现Error Establishing Database Connection问题
    Linux发行版时间线分支图最新版
    rem.js,移动多终端适配
    几种常用JavaScript设计模式es6
    文件上传,8种场景
    react动态添加样式:style和className
    记录我的 python 学习历程-Day13 匿名函数、内置函数 II、闭包
    记录我的 python 学习历程-Day12 生成器/推导式/内置函数Ⅰ
    记录我的 python 学习历程-Day11 两个被忽视的坑、补充知识点、函数名的应用、新版格式化输出、迭代器
    记录我的 python 学习历程-Day10 函数进阶
  • 原文地址:https://www.cnblogs.com/XJJD/p/7291578.html
Copyright © 2011-2022 走看看