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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值