FreeMarker最简单的例子(2)

二、通过String来创建模版对象,并执行插值处理

代码:

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.abc;  
  2.   
  3.    
  4.   
  5. import freemarker.template.Template;   
  6.   
  7.    
  8.   
  9. import java.io.OutputStreamWriter;   
  10.   
  11. import java.io.StringReader;   
  12.   
  13. import java.util.HashMap;   
  14.   
  15. import java.util.Map;   
  16.   
  17.    
  18.   
  19. public class Test2 {   
  20.   
  21.     public static void main(String[] args) throws Exception{   
  22.   
  23.         //创建一个模版对象   
  24.   
  25.         Template t = new Template(null, new StringReader("用户名:${user} URL:${url} 姓名:${website}"), null);   
  26.   
  27.         //创建插值的Map   
  28.   
  29.         Map<String, String> map = new HashMap<String, String>();   
  30.   
  31.         map.put("user", "Zheng");   
  32.   
  33.         map.put("url", "http://www.jebao.net/");   
  34.   
  35.         map.put("website", "金额宝");   
  36.   
  37.         //执行插值,并输出到指定的输出流中   
  38.   
  39.         t.process(map, new OutputStreamWriter(System.out));   
  40.   
  41.     }   
  42.   
  43. }  

运行结果:

用户名:Zheng

URL:http://www.jebao.NET/

姓名:金额宝

原文地址:https://www.cnblogs.com/grimm/p/6732628.html