隨筆 - 119  文章 - 3173  trackbacks - 0
          <2007年5月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          交友莫獨酒,茅臺西鳳游。
          口干古井貢,心徜洋河流。
          稱多情杜康,趟無量雙溝。
          贊中華巍巍,無此不銷愁。

          常用鏈接

          留言簿(68)

          隨筆分類(136)

          隨筆檔案(122)

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 527033
          • 排名 - 92

          最新評論

          FreeMarkerTest:
          ?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?


          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>??


          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>??


          freemarker支持多語言國際化,只要把模板名稱安裝資源文件的寫法就可以了,也就是name_語言_國家地區.ftl

          如果找不到對應的語言,就會用默認語言的模板。

          順便抱怨一下,freemarker對于空值的處理太霸道了,沒有值你就寫個空或者寫KEY也可以啊,弄一堆出錯信息在那。。。。。。。。。。。。。。
          posted on 2007-05-08 15:23 交口稱贊 閱讀(3723) 評論(5)  編輯  收藏 所屬分類: freemarker

          FeedBack:
          # re: 用freemarker生產靜態頁面文件,支持多語言 2007-05-08 15:35 killer->
          順便抱怨一下,freemarker對于空值的處理太霸道了,沒有值你就寫個空或者寫KEY也可以啊,弄一堆出錯信息在那。。。。。。。。。。。。。。

          這正是我喜歡freemarker的地方不像velocity那樣, 什么反應也沒有,錯了都不知道.  回復  更多評論
            
          # re: 用freemarker生產靜態頁面文件,支持多語言 2007-05-08 15:37 交口稱贊
          呵呵,我倒是希望他把KEY值留在那,就像做RCP開發時候那樣。
          這樣比較好。。。。。。

          留空確實不好。。。。  回復  更多評論
            
          # re: 用freemarker生產靜態頁面文件,支持多語言 2007-05-09 00:05 yadan
          加上個! 后面還可以帶任何默認值 很喜歡這種方式。   回復  更多評論
            
          # re: 用freemarker生產靜態頁面文件,支持多語言 2009-03-09 18:34 097
          你好,請問用FREEMARKER在WEB程序中生產靜態頁面模板文件怎么獲取?  回復  更多評論
            
          # re: 用freemarker生產靜態頁面文件,支持多語言 2011-11-22 09:16 q
          謝謝樓主,正好解決了我的問題。  回復  更多評論
            
          主站蜘蛛池模板: 扎鲁特旗| 景德镇市| 景泰县| 辉南县| 略阳县| 佛坪县| 廊坊市| 博罗县| 林西县| 白城市| 郧西县| 招远市| 马鞍山市| 马山县| 漳平市| 义乌市| 芜湖市| 婺源县| 乌拉特后旗| 正定县| 安塞县| 定西市| 沈丘县| 五常市| 两当县| 奉化市| 吐鲁番市| 沅陵县| 湖州市| 乐业县| 南昌市| 美姑县| 明光市| 全州县| 航空| 阜新| 晋中市| 新田县| 获嘉县| 神木县| 宜良县|