首页 > 代码库 > 字符串作为freemarker模板的简单实现例子

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

原文:http://blog.csdn.net/5iasp/article/details/27181365

 

package com.test.demo;

import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class TestFreemarker {

    /**
     * @param args
     */
    public static void main(String[] args) {

        
        Configuration cfg = new Configuration();
        StringTemplateLoader stringLoader = new StringTemplateLoader();
        String templateContent="欢迎:${name}";
        stringLoader.putTemplate("myTemplate",templateContent);
        
        cfg.setTemplateLoader(stringLoader);
        
        try {
            Template template = cfg.getTemplate("myTemplate","utf-8");
            Map root = new HashMap();  
            root.put("name", "javaboy2012");
            
            StringWriter writer = new StringWriter();  
            try {
                template.process(root, writer);
                System.out.println(writer.toString());  
            } catch (TemplateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
      
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }





    }

}

 

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