利用freemarker、java生成html静态页面

来源:互联网 发布:谷歌看图软件下载 编辑:程序博客网 时间:2024/05/10 08:24

这几天在搞一个利用freemarker和java生成静态页面的东西,经过百度和自己的调试终于搞定,现在总结出核心代码分享。

 

Java代码  收藏代码
  1. /** 
  2.      * 生成静态页面主方法 
  3.      *  
  4.      * @param context 
  5.      *            ServletContext 
  6.      * @param data 
  7.      *            一个Map的数据结果集 
  8.      * @param templatePath 
  9.      *            ftl模版路径 
  10.      * @param targetHtmlPath 
  11.      *            生成静态页面的路径 
  12.      */  
  13.     public static void crateHTML(ServletContext context,  
  14.             Map<String, Object> data, String templatePath, String targetHtmlPath) {  
  15.         // 加载模版  
  16.         freemarkerCfg.setServletContextForTemplateLoading(context, "/");  
  17.         freemarkerCfg.setEncoding(Locale.getDefault(), "UTF-8");  
  18.         String filePath = ServletActionContext.getServletContext().getRealPath(  
  19.                 "/static");  
  20.         File file = new File(filePath);  
  21.         if(!file.exists() || !file.isDirectory()){  
  22.             file.mkdir();  
  23.         }  
  24.         File f = new File(file,"/all_css");  
  25.         if(!f.exists() || !f.isDirectory()){  
  26.             f.mkdir();  
  27.         }  
  28.         try {  
  29.             freemarkerCfg.setDirectoryForTemplateLoading(new File(filePath));  
  30.             // 设置包装器,并将对象包装为数据模型  
  31.             freemarkerCfg.setObjectWrapper(new DefaultObjectWrapper());  
  32.             // 获取模板,并设置编码方式,这个编码必须要与页面中的编码格式一致  
  33.             // 否则会出现乱码  
  34.             Template template = freemarkerCfg  
  35.                     .getTemplate(templatePath, "UTF-8");  
  36.             template.setEncoding("UTF-8");  
  37.             // 静态页面路径  
  38.             String htmlPath = filePath + "/" + targetHtmlPath;  
  39.             File htmlFile = new File(htmlPath);  
  40.             Writer out = new BufferedWriter(new OutputStreamWriter(  
  41.                     new FileOutputStream(htmlFile), "UTF-8"));  
  42.             // 处理模版  
  43.             template.process(data, out);  
  44.             out.flush();  
  45.             out.close();  
  46.               
  47.         } catch (Exception e) {  
  48.             e.printStackTrace();  
  49.         }  
  50.           
  51.     }  
  52.   
  53.     /**  
Java代码  收藏代码
  1. * 生成友情链接的静态页cuxiao.html  
  2. *   
  3. @param context  
  4. @param data  
  5. */  
  6. ublic static void createIndexFriendLink(ServletContext context,  
  7.     Map<String, Object> data) {  
  8. try {  
  9. //cuxiao.ftl是项目中建立的ftl文件,cuxiao.html是生成的静态页面名  
  10.     return crateHTML(context, data, "/cuxiao.ftl""cuxiao.html");  
  11. catch (Exception e) {  
  12.     e.printStackTrace();  
  13. }  
 

   最后调用方法,private Map<String, Object> pList = new HashMap<String, Object>();

Java代码  收藏代码
  1. List list = new ArrayList();  
  2. pList.put("list",list);  
  3. HttpServletRequest request = ServletActionContext.getRequest();  
  4.             pList.put("JspTaglibs"new TaglibFactory(request.getSession()  
  5.                     .getServletContext()));  
  6.              this.createIndexFriendLink(  
  7.                     ServletActionContext.getServletContext(), pList); 
  8. //页面介绍map时一定要与pList中的key一致  





  1. PS:原来写在了iteye上,现在公司只能上csdn,就搬到这里了,欢迎各位大神拍砖。
0 0