freemaker:通过map方式替换字符串
1. maven
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
2.
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
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