freemarker 替換字串,模板,可以用來動態生成程式碼
阿新 • • 發佈:2019-02-18
freemarker 通過字串模板生成,會把map中 name 對應的變數替換字串模板中的${name}
package com.thinkgem.jeesite.test.test; import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; import java.util.HashMap; import java.util.Map; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; public class aaa { public static void main(String[] args) throws TemplateException, IOException { Map<String, String> map=new HashMap<String, String>(); map.put("username", "lisi"); String templateString="${username}/zhangsan"; StringWriter result = new StringWriter(); Template t = new Template("name1", new StringReader(templateString), new Configuration()); t.process(map, result); System.out.println(result.toString()); } }