freemarker使用map替换字符串中的值

package demo01;

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

import freemarker.template.Template;
import freemarker.template.TemplateException;

public class test01 {

    public static void main(String[] args) {
        try {
            Template tmp = new Template(null, new StringReader("name:${user};URL:${url};uname:${name}"), null);
            Map<String, String> map = new HashMap<String, String>();
            map.put("user", "crd");
            map.put("url", "www.baidu.com");
            map.put("name", "baidu");
            tmp.process(map, new OutputStreamWriter(System.out));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
    }

}

 

后台打印:

name:crd;URL:www.baidu.com;uname:baidu

posted @ 2019-06-25 15:16  爱跳舞的程序员  阅读(1957)  评论(0编辑  收藏  举报