Rory's Blog
          Happy study,Happy work,Happy life
          posts - 22,  comments - 46,  trackbacks - 0
          首先感謝JScud提供的好文章?!?a class="" title="" target="">使用FreeMarker生成Html靜態文件(實例)》
          ????? 在我們的項目中也用到了Freemarker生成靜態文件。不過這里我要說的是編碼的問題。我們的項目使用的都是UTF-8編碼,我直接使用 飛云小俠 提供的方法生成的文件在UTF-8編碼下察看是亂碼,而GBK正常(后來發現因為我用的中文操作系統所以用GBK查看正常)。
          ????? 當然我把Freemarker的配置都改成了UTF-8,我的模版文件也是UTF-8編碼的。下面是原來的代碼
          ????public?void?setTemplatePath(Resource?templatePath)?{
          ????????
          this.templatePath?=?templatePath;
          ????????
          //設置freemarker的參數
          ????????freemarkerCfg?=?new?Configuration();
          ????????
          try?{
          ????????????freemarkerCfg.setDirectoryForTemplateLoading(
          this.templatePath.getFile());
          ????????????freemarkerCfg.setObjectWrapper(
          new?DefaultObjectWrapper());
          ????????????freemarkerCfg.setDefaultEncoding(
          "UTF-8");
          ????????}?
          catch?(IOException?ex)?{
          ????????????
          throw?new?SystemException("No?Directory?found,please?check?you?config.");
          ????????}
          ????}
          ????/**
          ?????*?生成靜態文件
          ?????*?
          @param?templateFileName?模版名稱eg:(biz/order.ftl)
          ?????*?
          @param?propMap?用于處理模板的屬性Object映射?
          ?????*?
          @param?htmlFilePath?要生成的靜態文件的路徑,相對設置中的根路徑,例如?"/biz/2006/5/"?
          ?????*?
          @param?htmlFileName?要生成的文件名,例如?"123.htm"?
          ?????*?
          @return
          ?????
          */
          ????
          private?boolean?buildHtml(String?templateFileName,Map?propMap,?String?htmlFilePath,String?htmlFileName){
          ????????
          try?{
          ????????????Template?template?
          =?freemarkerCfg.getTemplate(templateFileName);
          ????????????template.setEncoding(
          "UTF-8");
          ????????????
          //創建生成文件目錄
          ????????????creatDirs(buildPath.getFilename(),htmlFilePath);
          ????????????File?htmlFile?
          =?new?File(buildPath?+?htmlFilePath?+?htmlFileName);
          ????????????Writer?out?
          =?new?BufferedWriter(new?OutputStreamWriter(new?FileOutputStream(htmlFile)));
          ????????????template.process(propMap,out);
          ????????????out.flush();
          ????????????
          return?true;
          ????????}?
          catch?(TemplateException?ex){
          ????????????log.error(
          "Build?Error"+templateFileName,ex);
          ????????????
          return?false;
          ????????}?
          catch?(IOException?e)?{
          ????????????log.error(
          "Build?Error"+templateFileName,e);
          ????????????
          return?false;
          ????????}
          ????????
          ????}
          下面是修改之后的代碼
          ????/**
          ?????*?生成靜態文件
          ?????*?
          @param?templateFileName?模版名稱eg:(biz/order.ftl)
          ?????*?
          @param?propMap?用于處理模板的屬性Object映射?
          ?????*?
          @param?htmlFilePath?要生成的靜態文件的路徑,相對設置中的根路徑,例如?"/biz/2006/5/"?
          ?????*?
          @param?htmlFileName?要生成的文件名,例如?"123.htm"?
          ?????*?
          @return
          ?????
          */
          ????
          private?boolean?buildHtml(String?templateFileName,Map?propMap,?String?htmlFilePath,String?htmlFileName){
          ????????
          try?{
          ????????????Template?template?
          =?freemarkerCfg.getTemplate(templateFileName);
          ????????????template.setEncoding(
          "UTF-8");
          ????????????
          //創建生成文件目錄
          ????????????creatDirs(buildPath.getFilename(),htmlFilePath);
          ????????????File?htmlFile?
          =?new?File(buildPath?+?htmlFilePath?+?htmlFileName);
          ????????????Writer?out?
          =?new?BufferedWriter(new?OutputStreamWriter(new?FileOutputStream(htmlFile),"UTF-8"));
          ????????????template.process(propMap,out);
          ????????????out.flush();
          ????????????
          return?true;
          ????????}?
          catch?(TemplateException?ex){
          ????????????log.error(
          "Build?Error"+templateFileName,ex);
          ????????????
          return?false;
          ????????}?
          catch?(IOException?e)?{
          ????????????log.error(
          "Build?Error"+templateFileName,e);
          ????????????
          return?false;
          ????????}
          ????????
          ????}
          原因就在于OutputStreamWriter的不同構造方法

          OutputStreamWriter(OutputStream?out)
          ??????????創建使用默認字符編碼的 OutputStreamWriter。
          OutputStreamWriter(OutputStream?out, String?charsetName)
          ??????????創建使用指定字符集的 OutputStreamWriter。

          ?
          這個是中文JDK的文檔說明,剛開始我使用默認的構造函數,所以使用了系統默認的編碼,GBK,所以在生成靜態文件的時候把UTF-8內容用GBK編碼寫入了,所以在UTF-8下瀏覽就有問題。

          還有關于修改模版文件同樣也要注意這個問題。
          ????public?String?loadTemplate(String?templateName)?{
          ????????StringBuffer?sb?
          =?new?StringBuffer();
          ????????
          try?{
          ????????????File?file?
          =?new?File(templatePath+"/"+templateName);
          ????????????BufferedReader?reader?
          =?new?BufferedReader(new?InputStreamReader(new?FileInputStream(file),"UTF-8"));
          ????????????String?line?
          =?reader.readLine();
          ????????????
          while(line?!=?null)????{
          ????????????????sb.append(line);
          ????????????????sb.append(
          "\r\n");
          ????????????????line?
          =?reader.readLine();
          ????????????}
          ????????????reader.close();
          ????????}?
          catch?(IOException?e)?{
          ????????????
          throw?new?SystemException("Loading?template?Error:",e);
          ????????}
          ????????
          return?sb.toString();
          ????}
          ????public?void?saveTemplate(String?templateName,?String?templateContent)?{
          ????????
          try?{
          ????????????File?file?
          =?new?File(templatePath?+?"/"?+?templateName);
          ????????????Writer?out?
          =?new?BufferedWriter(new?OutputStreamWriter(new?FileOutputStream(file),"UTF-8"));
          ????????????out.write(templateContent);
          ????????????out.flush();
          ????????????
          //扔出templatesave事件
          ????????????TemplateSaveEvent?evt?=?new?TemplateSaveEvent();
          ????????????evt.setTemplateName(templateName);
          ????????????dispatchTemplateEvent(evt);
          ????????}?
          catch?(IOException?e)?{
          ????????????
          throw?new?SystemException("Write?template?Error",e);
          ????????}
          ????}

          posted on 2006-06-21 10:46 莫多 閱讀(2865) 評論(0)  編輯  收藏 所屬分類: Other 、Spring

          <2006年6月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678

          常用鏈接

          留言簿(1)

          隨筆分類(27)

          隨筆檔案(22)

          Friends

          搜索

          •  

          積分與排名

          • 積分 - 62245
          • 排名 - 845

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 和龙市| 清流县| 卢湾区| 衡阳市| 扎鲁特旗| 博湖县| 佛学| 普洱| 板桥市| 紫阳县| 嵩明县| 威信县| 汉寿县| 建湖县| 溧阳市| 高雄县| 岳阳县| 双城市| 桃源县| 宝坻区| 宁陵县| 从江县| 紫云| 隆尧县| 通辽市| 利津县| 阿合奇县| 二连浩特市| 天峨县| 禄劝| 石泉县| 沧源| 屏东县| 石河子市| 合水县| 旺苍县| 永定县| 库尔勒市| 阳东县| 怀集县| 南昌县|