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文章


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


          網站導航:
           
          主站蜘蛛池模板: 班戈县| 清镇市| 莱西市| 宜兰县| 苏尼特左旗| 高邮市| 峨山| 蒙城县| 子洲县| 修水县| 邓州市| 昭苏县| 宁德市| 和平县| 合川市| 镇雄县| 岑巩县| 金沙县| 柳江县| 冷水江市| 贵州省| 会宁县| 纳雍县| 汤阴县| 汨罗市| 鄱阳县| 兴城市| 白沙| 望城县| 济宁市| 曲阳县| 泾阳县| 莒南县| 河西区| 桐柏县| 华坪县| 福海县| 怀仁县| 原阳县| 蕲春县| 广河县|