字符串作为freemarker模板的简单实现例子

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.test.demo;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.StringWriter;  
  5. import java.util.HashMap;  
  6. import java.util.Map;  
  7.   
  8. import freemarker.cache.StringTemplateLoader;  
  9. import freemarker.template.Configuration;  
  10. import freemarker.template.Template;  
  11. import freemarker.template.TemplateException;  
  12.   
  13. public class TestFreemarker {  
  14.   
  15.     /** 
  16.      * @param args 
  17.      */  
  18.     public static void main(String[] args) {  
  19.   
  20.           
  21.         Configuration cfg = new Configuration();  
  22.         StringTemplateLoader stringLoader = new StringTemplateLoader();  
  23.         String templateContent="欢迎:${name}";  
  24.         stringLoader.putTemplate("myTemplate",templateContent);  
  25.           
  26.         cfg.setTemplateLoader(stringLoader);  
  27.           
  28.         try {  
  29.             Template template = cfg.getTemplate("myTemplate","utf-8");  
  30.             Map root = new HashMap();    
  31.             root.put("name""javaboy2012");  
  32.               
  33.             StringWriter writer = new StringWriter();    
  34.             try {  
  35.                 template.process(root, writer);  
  36.                 System.out.println(writer.toString());    
  37.             } catch (TemplateException e) {  
  38.                 // TODO Auto-generated catch block  
  39.                 e.printStackTrace();  
  40.             }    
  41.         
  42.               
  43.         } catch (IOException e) {  
  44.             // TODO Auto-generated catch block  
  45.             e.printStackTrace();  
  46.         }  
  47.   
  48.   
  49.   
  50.   
  51.   
  52.     }  
  53.   
  54. }  

转自:http://blog.csdn.net/5iasp/article/details/27181365
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值