抛砖:用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等。       
        比较这两种方法:
        方法一:在需要生成分页文件时,比较合适。
        方法二:可以象往常一样使用,一次需要生成多文件则不适合。

        不知各位在做这些项目时,使用甚么好方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值