freemarker、webwork 生成静态页

方法一:(这个方法借鉴freemarker的docs文档Programmer's Guide 的quick start部分,详细请看相关的文档。

 

public class HtmlTemplateGenerator {   
  
    Configuration cfg = null;   
       
    public HtmlTemplateGenerator(String templatePath) throws IOException {   
        cfg = new Configuration();   
        cfg.setDefaultEncoding("UTF-8");   
        cfg.setDirectoryForTemplateLoading(new File(templatePath));   
        cfg.setObjectWrapper(new DefaultObjectWrapper());   
    }   
       
    /**  
     * 生成静态文件  
     * @param ftlTemplate ftl模版文件  
     * @param contents    ftl要用到的动态内容  
     * @param savePath    文件保存路径  
     * @param saveFilename 保存文件名  
     * @throws IOException  
     * @throws TemplateException  
     */  
    public void create(String ftlTemplate, Map contents, String savePath, String saveFilename) throws IOException, TemplateException {   
        Template temp = cfg.getTemplate(ftlTemplate);   
        /* Merge data model with template */  
           
        String realPath = ServletActionContext.getServletContext().getRealPath(savePath);   
        System.out.println( saveFilename + ":" + realPath);   
        File file = new File(realPath);   
        if(!file.exists())   
            file.mkdirs();   
           
        Writer out = new OutputStreamWriter(new FileOutputStream(realPath + "/" + saveFilename),"UTF-8");   
        temp.process(contents, out);   
        out.flush();   
    }   
       
}      
 

        如果用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方法:
不知有无好的法子。

protected Writer getWriter() throws IOException {   
        String savePath = (String) ActionContext.getContext().getSession().get("SAVE_PATH");   
        String saveFilename = (String) ActionContext.getContext().getSession().get("SAVE_FILENAME");   
        String realPath = ServletActionContext.getServletContext().getRealPath(savePath);   
        System.out.println( saveFilename + ":" + realPath);   
        File file = new File(realPath);   
        if(!file.exists())   
            file.mkdirs();   
           
        return templateOut = new OutputStreamWriter(new 
                                           FileOutputStream(realPath + "/" + saveFilename),"UTF-8");   
    }   
 

        这里的路径和文件名通过webwork的session传入,不知有无其他好方法。
        如果生成文件的同时还需要看到生成的页面,则要改写“doExecute”:

template.process(model, getWriter());   
template.process(model, super.getWriter()); //添加这句   
templateOut.flush();  
 

生成的文件格式不限于html,可以是其他文件格式,如js,text等。       
比较这两种方法:
   方法一:在需要生成分页文件时,比较合适。
   方法二:可以象往常一样使用,一次需要生成多文件则不适合。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值