当前位置: 代码迷 >> HTML/CSS >> Freemaker生成静态html页面
  详细解决方案

Freemaker生成静态html页面

热度:149   发布时间:2012-06-30 17:20:12.0
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. ?????*?@param?htmlFileName(生成的静态文件名)?
  43. ?????*/??
  44. ????@SuppressWarnings("unchecked")????
  45. ????public?void?geneHtmlFile(String?templateFileName,?String?templateFilePath,?Map?contextMap,????
  46. ????????????String?htmlFilePath,?String?htmlFileName)?{????
  47. ????
  48. ????????try?{????
  49. ????????????Template?t?=?this.getFreeMarkerCFG(templateFilePath).getTemplate(templateFileName);????
  50. ????????????//?如果根路径存在,则递归创建子目录?? ??
  51. ????????????this.creatDirs(htmlFilePath);????
  52. ????????????File?afile?=?new?File(htmlFilePath?+?"/"?+?htmlFileName);????
  53. ????????????Writer?out?=?new?BufferedWriter(new?OutputStreamWriter(????
  54. ????????????????????new?FileOutputStream(afile)));????
  55. ????????????t.process(contextMap,?out);????
  56. ????????????out.flush();????
  57. ????????????out.close();????
  58. ????????}?catch?(TemplateException?e)?{????
  59. ????????????System.out.print(e.getMessage());????
  60. ????????}?catch?(IOException?e)?{????
  61. ????????????System.out.print(e.getMessage());????
  62. ????????}?catch?(Exception?e)?{????
  63. ????????????System.out.print(e.getMessage());????
  64. ????????}????
  65. ????}????
  66. ????
  67. ????/**??
  68. ?????*???
  69. ?????*?获取freemarker的配置,freemarker本身支持classpath,目录或从ServletContext获取.??
  70. ?????*???
  71. ?????*?@param?templateFilePath??
  72. ?????*????????????获取模板路径??
  73. ?????*?@return?Configuration?返回freemaker的配置属性??
  74. ?????*?@throws?Exception??
  75. ?????*/????
  76. ????private?Configuration?getFreeMarkerCFG(String?templateFilePath)????
  77. ????????????throws?Exception?{????
  78. ????????if?(null?==?this.config)?{????
  79. ????
  80. ????????????this.config?=?new?Configuration();????
  81. ????????????try?{????
  82. ????????????????this.config.setDirectoryForTemplateLoading(new?File(????
  83. ????????????????????????templateFilePath));????
  84. ????????????}?catch?(Exception?ex)?{????
  85. ????????????????throw?ex;????
  86. ????????????}????
  87. ????????}????
  88. ????????return?this.config;????
  89. ????}????
  90. ??
  91. }??
  相关解决方案