freemarker 模版静态化

来源:互联网 发布:校园网络安全教育讲座 编辑:程序博客网 时间:2024/06/06 03:13
  1. public void consultaionToHtml(String cid) {  
  2.         try {  
  3.             this.publicOpenIdPropertiesUtils.getPropertiesValue("aloan.openid");  
  4.             String cityHtml="";  
  5.             GDSMConsultaion vo = gdsmconsultaionService.getObjectById(GDSMConsultaion.class, cid);  
  6.             String remarks="";  
  7.             if(null!=vo&&null!=vo.getRemarks()){  
  8.                 if(vo.getRemarks().equals("0")){  
  9.                     remarks="以上文章内容来源于网络,不代表本公司立场和观点,如有侵权请与我们联系。";  
  10.                 }else if(vo.getRemarks().equals("1")){  
  11.                     remarks="以上文章内容来源于宜信月刊(微信ID:yixinyuekan)。";  
  12.                 }else if(vo.getRemarks().equals("2")){  
  13.                     remarks="以上内容为原创内容,转发请注明来自宜信普惠金融家。";  
  14.                 }  
  15.             }  
  16.             if(null!=vo&&vo.getConsultaionType().equals("cheats")&&vo.getContentLink()==null){  
  17.                 Map map = new HashMap();  
  18.                 map.put("consultaionId",vo.getId());  
  19.                 map.put("title",vo.getTitle());  
  20.                 map.put("time",StaticMethod.formatDateToString(vo.getCreateTime(), "yyyy-MM-dd"));  
  21.                 map.put("content",vo.getContent().replaceAll(ResourceLocator.GEDAI_CONSULTAION_PIC_PATH,getSiteUrl()+"/"+ResourceLocator.GEDAI_CONSULTAION_PIC_PATH));  
  22.                 map.put("path",getSiteUrl());  
  23.                 map.put("remarks",remarks);  
  24.                 String path = ResourceLocator.getResourceAbsoluteLocationPath(ResourceLocator.GEDAI_CONSULTAION_HTML_PATH);  
  25.                 this.ouputFile("comCheats.ftl", vo.getId()+".html", map,path+"/cheats/"+StaticMethod.formatDateToString(vo.getCreateTime(), "yyyy-MM"));  
  26.             }else  if(null!=vo&&!vo.getConsultaionType().equals("cheats")){  
  27.                 String functiontype="";  
  28.                 if(vo.getConsultaionType().equals("essay")){  
  29.                     functiontype="ESSAY";  
  30.                 }else if(vo.getConsultaionType().equals("info")){  
  31.                     functiontype="CONSULTAION";  
  32.                 }  
  33.                 List<GDSMCity> cityList=gdManagerService.getGDCityLevel1();  
  34.                 for(GDSMCity city : cityList){  
  35.                     cityHtml=cityHtml+"<option value='"+city.getCityCode()+"'>"+city.getCityName()+"</option>";  
  36.                 }  
  37.                 String imgurl="";  
  38.                 if(vo!=null&&vo.getLogoUrl()!=null){  
  39.                     imgurl=getSiteUrl() +"/"+ ResourceLocator.GEDAI_CONSULTAION_PIC_PATH +"/"+vo.getLogoUrl();  
  40.                 }else{  
  41.                     imgurl=getSiteUrl() + "/img/weixin/gdSelfMarketing/logo1.png";  
  42.                 }  
  43.                 Map map = new HashMap();  
  44.                 map.put("publicOpenId",publicOpenId);  
  45.                 map.put("consultaionId",vo.getId());  
  46.                 map.put("title",vo.getTitle());  
  47.                 map.put("time",StaticMethod.formatDateToString(vo.getCreateTime(), "yyyy-MM-dd"));  
  48.                 map.put("content",vo.getContent().replaceAll(ResourceLocator.GEDAI_CONSULTAION_PIC_PATH,getSiteUrl()+"/"+ResourceLocator.GEDAI_CONSULTAION_PIC_PATH));  
  49.                 map.put("cityHtml",cityHtml);  
  50.                 map.put("path",getSiteUrl());  
  51.                 map.put("shareImageUrl",imgurl);  
  52.                 map.put("shareDesc","宜信为没有受到传统金融体系重视的高成长性人群提供普惠金融服务宜信普惠。");  
  53.                 map.put("remarks",remarks);  
  54.                 map.put("functiontype",functiontype);  
  55.                 String path = ResourceLocator.getResourceAbsoluteLocationPath(ResourceLocator.GEDAI_CONSULTAION_HTML_PATH);  
  56.                 for(GDSMCity city : cityList){  
  57.                     this.ouputFile("comProduct.ftl", vo.getId()+".html", map,path+"/"+city.getCityCode()+"/"+StaticMethod.formatDateToString(vo.getCreateTime(), "yyyy-MM"));  
  58.                 }  
  59.                 logger.info("资讯【ID={}】静态化生成成功。", cid);  
  60.             }  
  61.         } catch (Exception e) {  
  62.             logger.info("资讯【ID={}】静态化生成失败。",cid);  
  63.             logger.error(e.getMessage(), e);  
  64.         }  
  65.     }  
  66.   
  67.     public void ouputFile(String ftlName, String fileName,  Map<String, Object> map,String path) throws Exception{  
  68.         //创建fm的配置  
  69.         Configuration config = new Configuration();  
  70.         //指定默认编码格式  
  71.         config.setDefaultEncoding("UTF-8");  
  72.         //设置模板的包路径  
  73.         config.setClassForTemplateLoading(this.getClass(), "/com/yixin/gdSelfMarketing/ftl");  
  74.         //获得包的模板  
  75.         Template template = config.getTemplate(ftlName);  
  76.         //指定文件输出的路径  
  77.         File file=new File(path+"/"+fileName);  
  78.         File parent = file.getParentFile();  
  79.         if (!parent.exists()) {  
  80.             parent.mkdirs();  
  81.         }  
  82.         //定义输出流,注意的必须指定编码  
  83.         Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(path+"/"+fileName)),"UTF-8"));  
  84.         //生成模板  
  85.         template.process(map, writer);  
  86.         writer.close();  
  87.     }  
2.生成html文件


3.访问生成的html文件