JAVA流通橋

          JAVA啟發者

          統計

          留言簿(3)

          AJAX相關網址

          Eclipse相關網址

          Hibernate

          java相關網址

          LINUX相關網址

          webwork相關網址

          友好鏈接

          閱讀排行榜

          評論排行榜

          用freemarker、webwork發布靜態頁面的方法

          方法一:(這個方法借鑒freemarker的docs文檔Programmer's Guide 的quick start部分,詳細請看相關的文檔。

          java 代碼

           

          1. public class HtmlTemplateGenerator {   
          2.   
          3.     Configuration cfg = null;   
          4.        
          5.     public HtmlTemplateGenerator(String templatePath) throws IOException {   
          6.         cfg = new Configuration();   
          7.         cfg.setDefaultEncoding("UTF-8");   
          8.         cfg.setDirectoryForTemplateLoading(new File(templatePath));   
          9.         cfg.setObjectWrapper(new DefaultObjectWrapper());   
          10.     }   
          11.        
          12.     /**  
          13.      * 生成靜態文件  
          14.      * @param ftlTemplate ftl模版文件  
          15.      * @param contents    ftl要用到的動態內容  
          16.      * @param savePath    文件保存路徑  
          17.      * @param saveFilename 保存文件名  
          18.      * @throws IOException  
          19.      * @throws TemplateException  
          20.      */  
          21.     public void create(String ftlTemplate, Map contents, String savePath, String saveFilename) throws IOException, TemplateException {   
          22.         Template temp = cfg.getTemplate(ftlTemplate);   
          23.         /* Merge data model with template */  
          24.            
          25.         String realPath = ServletActionContext.getServletContext().getRealPath(savePath);   
          26.         System.out.println( saveFilename + ":" + realPath);   
          27.         File file = new File(realPath);   
          28.         if(!file.exists())   
          29.             file.mkdirs();   
          30.            
          31.         Writer out = new OutputStreamWriter(new FileOutputStream(realPath + "/" + saveFilename),"UTF-8");   
          32.         temp.process(contents, out);   
          33.         out.flush();   
          34.     }   
          35.        
          36. }     

                  如果用spring,可以將它配置成bean,然后在其他地方使用。第五行的 templatePath 是模版文件的路徑,比如/WEB-INF/template。
                  action中的使用:HtmlTemplateGenerator.create("html/magazine/search.ftl", null, "/magazine", "search.html"); 其中“html/magazine/search.ftl”是在“/WEB-INF/template”目錄下。這里還需要注意的是。模版文件(search.ftl)中如果還要引用其他文件,它的路徑也是不需要添加總路徑“/WEB-INF/template”。

          方法二:繼承webwork的FreemarkerResult,改寫getWriter方法:
          不知有無好的法子。

          java 代碼
          1. protected Writer getWriter() throws IOException {   
          2.         String savePath = (String) ActionContext.getContext().getSession().get("SAVE_PATH");   
          3.         String saveFilename = (String) ActionContext.getContext().getSession().get("SAVE_FILENAME");   
          4.         String realPath = ServletActionContext.getServletContext().getRealPath(savePath);   
          5.         System.out.println( saveFilename + ":" + realPath);   
          6.         File file = new File(realPath);   
          7.         if(!file.exists())   
          8.             file.mkdirs();   
          9.            
          10.         return templateOut = new OutputStreamWriter(new FileOutputStream(realPath + "/" + saveFilename),"UTF-8");   
          11.     }  

                  這里的路徑和文件名通過webwork的session傳入,不知有無其他好方法。
                  如果生成文件的同時還需要看到生成的頁面,則要改寫“doExecute”:

          java 代碼
          1. template.process(model, getWriter());   
          2. template.process(model, super.getWriter()); //添加這句   
          3. templateOut.flush();  

           

                  生成的文件格式不限于html,可以是其他文件格式,如js,text等。       
                  比較這兩種方法:
                  方法一:在需要生成分頁文件時,比較合適。
                  方法二:可以象往常一樣使用,一次需要生成多文件則不適合。

                  不知各位在做這些項目時,使用甚么好方法。

          posted on 2007-04-18 11:56 朱巖 閱讀(416) 評論(0)  編輯  收藏 所屬分類: freemarker文章


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 西丰县| 英山县| 陆河县| 民乐县| 临漳县| 广州市| 股票| 韶关市| 邢台县| 罗田县| 夏河县| 永平县| 德昌县| 忻城县| 东兴市| 临高县| 开封市| 南京市| 温州市| 肃南| 镇康县| 金寨县| 辽阳市| 碌曲县| 登封市| 高密市| 南开区| 海丰县| 宽城| 新丰县| 固安县| 淮南市| 云南省| 湖北省| 镇远县| 紫阳县| 霍州市| 安新县| 花莲县| 秦皇岛市| 同江市|