FreeMarker解析字符串模板

  1. /**
     * Created: 2007-2-1
     * Modified: 2007-2-1
     *
     * Author: Keven Chen
     */
    package freemarker.loader;

    import java.io.IOException;
    import java.io.Reader;
    import java.io.StringReader;
    import java.util.HashMap;
    import java.util.Map;

    import freemarker.cache.TemplateLoader;

    /**
     * @author Keven Chen
     * @version $Revision 1.0 $
     *
     */
    public class StringTemplateLoader implements TemplateLoader {
     private static final String DEFAULT_TEMPLATE_KEY = "_default_template_key";
     private Map templates = new HashMap();
     
     public StringTemplateLoader(String defaultTemplate){
      if(defaultTemplate != null && !defaultTemplate.equals("")){
       templates.put(DEFAULT_TEMPLATE_KEY, defaultTemplate);
      }
     }
     
     public void AddTemplate(String name,String template){
      if(name == null || template == null
        || name.equals("") || template.equals("")){
       return ;
      }
      if(!templates.containsKey(name)){
       templates.put(name, template);
      }
     }
     
     public void closeTemplateSource(Object templateSource) throws IOException {
      
     }

     public Object findTemplateSource(String name) throws IOException {
      if(name == null || name.equals("")){
       name = DEFAULT_TEMPLATE_KEY;
      }
      return templates.get(name);
     }

     public long getLastModified(Object templateSource) {
      return 0;
     }

     public Reader getReader(Object templateSource, String encoding)
       throws IOException {
      return new StringReader((String) templateSource);
     }

    }

      
  2. /**  
  3.  * Created: 2007-2-1  
  4.  * Modified: 2007-2-1  
  5.  *   
  6.  * Author: Keven Chen  
  7.  */  
  8. package freemarker;   
  9.   
  10. import java.io.File;   
  11. import java.io.OutputStreamWriter;   
  12. import java.io.StringWriter;   
  13. import java.util.Date;   
  14. import java.util.HashMap;   
  15. import java.util.Map;   
  16.   
  17. import freemarker.loader.ClassPathTemplateLoader;   
  18. import freemarker.loader.StringTemplateLoader;   
  19. import freemarker.template.Configuration;   
  20. import freemarker.template.Template;   
  21.   
  22. /**  
  23.  * @author Keven Chen  
  24.  * @version $Revision 1.0 $  
  25.  *  
  26.  */  
  27. public class Test {   
  28.   
  29.     public static void main(String[] args) throws Exception {   
  30.         Configuration cfg = new Configuration();   
  31.         cfg.setTemplateLoader(new StringTemplateLoader("欢迎:${user}"));   
  32.         cfg.setDefaultEncoding("UTF-8");   
  33.   
  34.         Template template = cfg.getTemplate("");   
  35.            
  36.         Map root = new HashMap();   
  37.         root.put("user""Keven Chen");   
  38.            
  39.         StringWriter writer = new StringWriter();   
  40.         template.process(root, writer);   
  41.         System.out.println(writer.toString());         
  42.     }   
  43.   
  44. }   
  45.   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值