道非道 非常道

勤思、谨言、慎行、厚积、薄发

统计

web

天圆

经济 政治 军事

键康

[转]jsp生成静态页程序

package   com.r.util;  
   
  import   java.io.*;  
  import   java.util.Map;  
   
  import   org.apache.commons.logging.Log;  
  import   org.apache.commons.logging.LogFactory;  
  import   freemarker.template.*;  
   
  public   class   MakeHtmlFile   {  
   
  private   final   Log   logger   =   LogFactory.getLog(getClass());  
   
  private   Configuration   freemarkerCfg   =   null;  
   
  private   String   templatePath   =   "";  
   
  private   String   realBuildPath   =   "";  
   
  /**  
    *    
    *   @param   realBuildPath根目录绝对路径  
    *   @param   templatePath模板相对路径,不包含模板名称  
    */  
  public   MakeHtmlFile(String   realBuildPath,   String   templatePath)   {  
  this.templatePath   =   templatePath;  
  this.realBuildPath   =   realBuildPath;  
  setTemplatePath();  
  }  
   
  /**  
    *    
    *   @param   templatePath:模板绝对路径  
    */  
  private   void   setTemplatePath()   {  
  //   设置freemarker的参数  
  freemarkerCfg   =   new   Configuration();  
  try   {  
  File   file   =   new   File(realBuildPath+templatePath);  
  freemarkerCfg  
  .setDirectoryForTemplateLoading(file);  
  freemarkerCfg.setObjectWrapper(new   DefaultObjectWrapper());  
  freemarkerCfg.setDefaultEncoding("UTF-8");  
  }   catch   (IOException   ex)   {  
  System.out.println("No   Directory   found,please   check   you   config."n"  
  +   realBuildPath+templatePath);  
  }  
  }  
   
  /**  
    *   生成静态文件  
    *    
    *   @param   templateFileName  
    *                         模版名称eg:(biz/order.ftl)  
    *   @param   propMap  
    *                         用于处理模板的属性Object映射  
    *   @param   htmlFilePath  
    *                         要生成的静态文件的路径,相对设置中的根路径,例如   "/biz/2006/5/"  
    *   @param   htmlFileName  
    *                         要生成的文件名,例如   "123.htm"  
    *   @return  
    */  
  public   boolean   buildHtml(String   templateFileName,   Map   propMap,  
  String   htmlFilePath,   String   htmlFileName)   {  
  try   {  
  Template   template   =   freemarkerCfg.getTemplate(templateFileName);  
  template.setEncoding("UTF-8");  
  //   创建生成文件目录  
  creatDirs(realBuildPath,   htmlFilePath);  
  File   htmlFile   =   new   File(realBuildPath   +   htmlFilePath  
  +   htmlFileName);  
  Writer   out   =   new   BufferedWriter(new   OutputStreamWriter(  
  new   FileOutputStream(htmlFile),   "UTF-8"));  
  template.process(propMap,   out);  
  out.flush();  
  return   true;  
  }   catch   (TemplateException   ex)   {  
  ex.printStackTrace();  
  logger.error("Build   Error"   +   templateFileName,   ex);  
  return   false;  
  }   catch   (IOException   e)   {  
  logger.error("Build   Error"   +   templateFileName,   e);  
  return   false;  
  }  
   
  }  
   
  /**  
    *   创建多级目录  
    *    
    *   @param   aParentDir  
    *                         String  
    *   @param   aSubDir  
    *                         以   /   开头  
    *   @return   boolean   是否成功  
    */  
  public   static   boolean   creatDirs(String   aParentDir,   String   aSubDir)   {  
  File   aFile   =   new   File(aParentDir);  
  if   (aFile.exists())   {  
  File   aSubFile   =   new   File(aParentDir   +   aSubDir);  
  if   (!aSubFile.exists())   {  
  return   aSubFile.mkdirs();  
  }   else   {  
  return   true;  
  }  
  }   else   {  
  return   false;  
  }  
  }  
  } 


别着急,等我学会了,我一步步说明一什么意思,

posted on 2008-12-14 10:55 星期五 阅读(352) 评论(0)  编辑  收藏 所属分类: J2EE


只有注册用户登录后才能发表评论。


网站导航: