首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > CSS >

Freemaker生成静态html页面

2012-06-30 
Freemaker生成静态html页面 .FreeMarker 是一个用Java编写的模板引擎,主要用来生成HTML Web页面,特别是基

Freemaker生成静态html页面 .

FreeMarker 是一个用Java编写的模板引擎,主要用来生成HTML Web页面,特别是基于MVC模式的应用程序。虽然FreeMarker具有一些编程的能力,但不像PHP,通常由Java程序准备要显示的数据,由 FreeMarker模板生成页面。 FreeMarker可以作为Web应用框架一个组件,但它与容器无关,在非Web应用程序环境也能工作的很好。 FreeMarker适合作为MVC的视图组件,还能在模板中使用JSP标记库。

[java] view plaincopyprint?
  1. ???import?java.io.BufferedWriter;??
  2. import?java.io.File;?? import?java.io.FileOutputStream;??
  3. import?java.io.IOException;?? import?java.io.OutputStreamWriter;??
  4. import?java.io.Writer;?? import?java.util.Map;??
  5. ??import?freemarker.template.Configuration;??
  6. import?freemarker.template.Template;?? import?freemarker.template.TemplateException;??
  7. ??/**?
  8. ?*?freemarker生成静态html??*?@author?lpz?
  9. ?*??*/??
  10. public?class?GeneratorHtml?{?? ????private?Configuration?config?=?null;????
  11. ????????/**??
  12. ?????*?如果目录不存在,则自动创建??????*?@param?path??
  13. ?????*?@return?boolean?是否成功???????*/????
  14. ????private?boolean?creatDirs(String?path)?{???? ????????File?aFile?=?new?File(path);????
  15. ????????if?(!aFile.exists())?{???? ????????????return?aFile.mkdirs();????
  16. ????????}?else?{???? ????????????return?true;????
  17. ????????}????????}????
  18. ????????/**?
  19. ?????*?模板生成静态html的方法??????*?@param?templateFileName(模板文件名)?
  20. ?????*?@param?templateFilePath(指定模板目录)??????*?@param?contextMap?(用于处理模板的属性Object映射)?
  21. ?????*?@param?htmlFilePath(指定生成静态html的目录)??????*?@param?htmlFileName(生成的静态文件名)?
  22. ?????*/??????@SuppressWarnings("unchecked")????
  23. ????public?void?geneHtmlFile(String?templateFileName,?String?templateFilePath,?Map?contextMap,???? ????????????String?htmlFilePath,?String?htmlFileName)?{????
  24. ????????????try?{????
  25. ????????????Template?t?=?this.getFreeMarkerCFG(templateFilePath).getTemplate(templateFileName);???? ????????????//?如果根路径存在,则递归创建子目录?? ??
  26. ????????????this.creatDirs(htmlFilePath);???? ????????????File?afile?=?new?File(htmlFilePath?+?"/"?+?htmlFileName);????
  27. ????????????Writer?out?=?new?BufferedWriter(new?OutputStreamWriter(???? ????????????????????new?FileOutputStream(afile)));????
  28. ????????????t.process(contextMap,?out);????????????????out.flush();????
  29. ????????????out.close();????????????}?catch?(TemplateException?e)?{????
  30. ????????????System.out.print(e.getMessage());????????????}?catch?(IOException?e)?{????
  31. ????????????System.out.print(e.getMessage());????????????}?catch?(Exception?e)?{????
  32. ????????????System.out.print(e.getMessage());????????????}????
  33. ????}????????
  34. ????/**???????*???
  35. ?????*?获取freemarker的配置,freemarker本身支持classpath,目录或从ServletContext获取.???????*???
  36. ?????*?@param?templateFilePath???????*????????????获取模板路径??
  37. ?????*?@return?Configuration?返回freemaker的配置属性???????*?@throws?Exception??
  38. ?????*/????????private?Configuration?getFreeMarkerCFG(String?templateFilePath)????
  39. ????????????throws?Exception?{???? ????????if?(null?==?this.config)?{????
  40. ????????????????this.config?=?new?Configuration();????
  41. ????????????try?{???? ????????????????this.config.setDirectoryForTemplateLoading(new?File(????
  42. ????????????????????????templateFilePath));????????????????}?catch?(Exception?ex)?{????
  43. ????????????????throw?ex;???? ????????????}????
  44. ????????}????????????return?this.config;????
  45. ????}??????
  46. }??

热点排行