日期:2014-05-17  浏览次数:20667 次

Freemaker生成静态html页面 .

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

[java] view plaincopyprint?
  1. ???
  2. import?java.io.BufferedWriter;??
  3. import?java.io.File;??
  4. import?java.io.FileOutputStream;??
  5. import?java.io.IOException;??
  6. import?java.io.OutputStreamWriter;??
  7. import?java.io.Writer;??
  8. import?java.util.Map;??
  9. ??
  10. import?freemarker.template.Configuration;??
  11. import?freemarker.template.Template;??
  12. import?freemarker.template.TemplateException;??
  13. ??
  14. /**?
  15. ?*?freemarker生成静态html?
  16. ?*?@author?lpz?
  17. ?*?
  18. ?*/??
  19. public?class?GeneratorHtml?{??
  20. ????private?Configuration?config?=?null;????
  21. ????
  22. ????/**??
  23. ?????*?如果目录不存在,则自动创建?
  24. ?????*?@param?path??
  25. ?????*?@return?boolean?是否成功??
  26. ?????*/????
  27. ????private?boolean?creatDirs(String?path)?{????
  28. ????????File?aFile?=?new?File(path);????
  29. ????????if?(!aFile.exists())?{????
  30. ????????????return?aFile.mkdirs();????
  31. ????????}?else?{????
  32. ????????????return?true;????
  33. ????????}????
  34. ????}????
  35. ????
  36. ????/**?
  37. ?????*?模板生成静态html的方法?
  38. ?????*?@param?templateFileName(模板文件名)?
  39. ?????*?@param?templateFilePath(指定模板目录)?
  40. ?????*?@param?contextMap?(用于处理模板的属性Object映射)?
  41. ?????*?@param?htmlFilePath(指定生成静态html的目录)?
  42. ?????*?@para