?1?import?java.io.BufferedWriter;
?2?import?java.io.File;
?3?import?java.io.FileOutputStream;
?4?import?java.io.OutputStreamWriter;
?5?import?java.io.Writer;
?6?import?java.util.HashMap;
?7?import?java.util.Locale;
?8?
?9?import?freemarker.template.Configuration;
10?import?freemarker.template.Template;
11?
12?public?class?FreeMarkerTest?{
13?
14?????public?static?void?main(String[]?args)?{
15?????????FreeMarkerTest?test?=?new?FreeMarkerTest();
16?????????test.getFile();
17?????????test.getFile(Locale.JAPAN);
18?????}
19?????
20?????public?void?getFile()?{
21?????????Configuration?freemarkerCfg?=?new?Configuration();
22?????????freemarkerCfg.setClassForTemplateLoading(this.getClass(),"/");
23?????????freemarkerCfg.setEncoding(Locale.getDefault(),"UTF-8");
24?????????Template?template;
25?????????try?{
26?????????????template?=?freemarkerCfg.getTemplate("t.ftl");
27?????????????template.setEncoding("UTF-8");
28?????????????File?htmlFile?=?new?File("t.html");
29?????????????Writer?out?=?new?BufferedWriter(new?OutputStreamWriter(
30?????????????????????new?FileOutputStream(htmlFile),?"UTF-8"));
31?????????????HashMap?propMap?=?new?HashMap();
32?????????????propMap.put("user",?"hermit");
33?????????????template.process(propMap,?out);
34?????????????out.flush();
35?????????}?catch?(Exception?e)?{
36?????????????e.printStackTrace();
37?????????}
38?????}
39?????
40?????public?void?getFile(Locale?loc)?{
41?????????Configuration?freemarkerCfg?=?new?Configuration();
42?????????freemarkerCfg.setClassForTemplateLoading(this.getClass(),"/");
43?????????freemarkerCfg.setEncoding(Locale.getDefault(),"UTF-8");
44?????????Template?template;
45?????????try?{
46?????????????template?=?freemarkerCfg.getTemplate("t.ftl",loc);
47?????????????template.setEncoding("UTF-8");
48?????????????File?htmlFile?=?new?File("t_"+loc.getLanguage()+"_"+loc.getCountry()+".html");
49?????????????Writer?out?=?new?BufferedWriter(new?OutputStreamWriter(
50?????????????????????new?FileOutputStream(htmlFile),?"UTF-8"));
51?????????????HashMap?propMap?=?new?HashMap();
52?????????????propMap.put("user",?"hermit");
53?????????????template.process(propMap,?out);
54?????????????out.flush();
55?????????}?catch?(Exception?e)?{
56?????????????e.printStackTrace();
57?????????}
58?????}
59?
60?}
61?
?2?import?java.io.File;
?3?import?java.io.FileOutputStream;
?4?import?java.io.OutputStreamWriter;
?5?import?java.io.Writer;
?6?import?java.util.HashMap;
?7?import?java.util.Locale;
?8?
?9?import?freemarker.template.Configuration;
10?import?freemarker.template.Template;
11?
12?public?class?FreeMarkerTest?{
13?
14?????public?static?void?main(String[]?args)?{
15?????????FreeMarkerTest?test?=?new?FreeMarkerTest();
16?????????test.getFile();
17?????????test.getFile(Locale.JAPAN);
18?????}
19?????
20?????public?void?getFile()?{
21?????????Configuration?freemarkerCfg?=?new?Configuration();
22?????????freemarkerCfg.setClassForTemplateLoading(this.getClass(),"/");
23?????????freemarkerCfg.setEncoding(Locale.getDefault(),"UTF-8");
24?????????Template?template;
25?????????try?{
26?????????????template?=?freemarkerCfg.getTemplate("t.ftl");
27?????????????template.setEncoding("UTF-8");
28?????????????File?htmlFile?=?new?File("t.html");
29?????????????Writer?out?=?new?BufferedWriter(new?OutputStreamWriter(
30?????????????????????new?FileOutputStream(htmlFile),?"UTF-8"));
31?????????????HashMap?propMap?=?new?HashMap();
32?????????????propMap.put("user",?"hermit");
33?????????????template.process(propMap,?out);
34?????????????out.flush();
35?????????}?catch?(Exception?e)?{
36?????????????e.printStackTrace();
37?????????}
38?????}
39?????
40?????public?void?getFile(Locale?loc)?{
41?????????Configuration?freemarkerCfg?=?new?Configuration();
42?????????freemarkerCfg.setClassForTemplateLoading(this.getClass(),"/");
43?????????freemarkerCfg.setEncoding(Locale.getDefault(),"UTF-8");
44?????????Template?template;
45?????????try?{
46?????????????template?=?freemarkerCfg.getTemplate("t.ftl",loc);
47?????????????template.setEncoding("UTF-8");
48?????????????File?htmlFile?=?new?File("t_"+loc.getLanguage()+"_"+loc.getCountry()+".html");
49?????????????Writer?out?=?new?BufferedWriter(new?OutputStreamWriter(
50?????????????????????new?FileOutputStream(htmlFile),?"UTF-8"));
51?????????????HashMap?propMap?=?new?HashMap();
52?????????????propMap.put("user",?"hermit");
53?????????????template.process(propMap,?out);
54?????????????out.flush();
55?????????}?catch?(Exception?e)?{
56?????????????e.printStackTrace();
57?????????}
58?????}
59?
60?}
61?
t.ftl
<html>
<head>
??<title>Welcome!</title>
??<META?HTTP-EQUIV="Content-Type"?CONTENT="text/html;?charset=utf-8">
</head>
<body>
??<h1>Welcome?${user}!</h1>
</body>
</html>??
<head>
??<title>Welcome!</title>
??<META?HTTP-EQUIV="Content-Type"?CONTENT="text/html;?charset=utf-8">
</head>
<body>
??<h1>Welcome?${user}!</h1>
</body>
</html>??
t_zh_CN.ftl
<html>
<head>
??<title>歡迎!</title>
??<META?HTTP-EQUIV="Content-Type"?CONTENT="text/html;?charset=utf-8">
</head>
<body>
??<h1>你好?${user}!</h1>
</body>
</html>??
<head>
??<title>歡迎!</title>
??<META?HTTP-EQUIV="Content-Type"?CONTENT="text/html;?charset=utf-8">
</head>
<body>
??<h1>你好?${user}!</h1>
</body>
</html>??
freemarker支持多語言國際化,只要把模板名稱安裝資源文件的寫法就可以了,也就是name_語言_國家地區(qū).ftl
如果找不到對(duì)應(yīng)的語言,就會(huì)用默認(rèn)語言的模板。
順便抱怨一下,freemarker對(duì)于空值的處理太霸道了,沒有值你就寫個(gè)空或者寫KEY也可以啊,弄一堆出錯(cuò)信息在那。。。。。。。。。。。。。。