<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Rory's Blog
    Happy study,Happy work,Happy life
    posts - 22,  comments - 46,  trackbacks - 0
    首先感謝JScud提供的好文章。《使用FreeMarker生成Html靜態(tài)文件(實(shí)例)
    ????? 在我們的項(xiàng)目中也用到了Freemarker生成靜態(tài)文件。不過這里我要說的是編碼的問題。我們的項(xiàng)目使用的都是UTF-8編碼,我直接使用 飛云小俠 提供的方法生成的文件在UTF-8編碼下察看是亂碼,而GBK正常(后來發(fā)現(xiàn)因?yàn)槲矣玫闹形牟僮飨到y(tǒng)所以用GBK查看正常)。
    ????? 當(dāng)然我把Freemarker的配置都改成了UTF-8,我的模版文件也是UTF-8編碼的。下面是原來的代碼
    ????public?void?setTemplatePath(Resource?templatePath)?{
    ????????
    this.templatePath?=?templatePath;
    ????????
    //設(shè)置freemarker的參數(shù)
    ????????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.");
    ????????}
    ????}
    ????/**
    ?????*?生成靜態(tài)文件
    ?????*?
    @param?templateFileName?模版名稱eg:(biz/order.ftl)
    ?????*?
    @param?propMap?用于處理模板的屬性O(shè)bject映射?
    ?????*?
    @param?htmlFilePath?要生成的靜態(tài)文件的路徑,相對設(shè)置中的根路徑,例如?"/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");
    ????????????
    //創(chuàng)建生成文件目錄
    ????????????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;
    ????????}
    ????????
    ????}
    下面是修改之后的代碼
    ????/**
    ?????*?生成靜態(tài)文件
    ?????*?
    @param?templateFileName?模版名稱eg:(biz/order.ftl)
    ?????*?
    @param?propMap?用于處理模板的屬性O(shè)bject映射?
    ?????*?
    @param?htmlFilePath?要生成的靜態(tài)文件的路徑,相對設(shè)置中的根路徑,例如?"/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");
    ????????????
    //創(chuàng)建生成文件目錄
    ????????????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的不同構(gòu)造方法

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

    ?
    這個是中文JDK的文檔說明,剛開始我使用默認(rèn)的構(gòu)造函數(shù),所以使用了系統(tǒng)默認(rèn)的編碼,GBK,所以在生成靜態(tài)文件的時候把UTF-8內(nèi)容用GBK編碼寫入了,所以在UTF-8下瀏覽就有問題。

    還有關(guān)于修改模版文件同樣也要注意這個問題。
    ????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 莫多 閱讀(2863) 評論(0)  編輯  收藏 所屬分類: OtherSpring

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

    常用鏈接

    留言簿(1)

    隨筆分類(27)

    隨筆檔案(22)

    Friends

    搜索

    •  

    積分與排名

    • 積分 - 62221
    • 排名 - 845

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 永久在线免费观看| 一级毛片成人免费看a| 久久国产精品成人片免费| 亚洲成AV人片在线观看WWW| 国产一级黄片儿免费看| 亚洲精品制服丝袜四区| 成人爽a毛片免费| 久久亚洲AV无码精品色午夜| 永久免费视频网站在线观看| 亚洲va久久久久| 国产中文字幕免费| 七次郎成人免费线路视频| 久久久久亚洲AV成人网人人网站 | 亚洲色www永久网站| 麻豆国产人免费人成免费视频| 亚洲私人无码综合久久网| 四虎AV永久在线精品免费观看| 青青久久精品国产免费看| 亚洲综合色婷婷七月丁香| 黄色片免费在线观看| 亚洲首页在线观看| 女性无套免费网站在线看| 一本大道一卡二大卡三卡免费 | 亚洲高清最新av网站| 三上悠亚在线观看免费| 亚洲日本乱码一区二区在线二产线 | 亚洲av无码专区首页| 国产精品亚洲精品日韩已方 | 色屁屁在线观看视频免费| 亚洲中文字幕在线乱码| 99无码人妻一区二区三区免费| 精品久久亚洲一级α| 亚洲精品乱码久久久久66| 99爱在线精品免费观看| 色吊丝免费观看网站| 久久亚洲精品人成综合网| 最新仑乱免费视频| 在线看片免费人成视频播| 自拍偷区亚洲国内自拍| 伊人亚洲综合青草青草久热| 综合在线免费视频|