论坛首页 入门技术论坛

freemarker生成静态页面

浏览 3304 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-11-01  

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

java 代码

 

	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”

方法二:继承webworkFreemarkerResult,改写getWriter方法:
不知有无好的法子。

java 代码

 

 

 

	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");    
	    }   

  

 

  

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

 

 

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

 

 

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

 

 

论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics