1 package cn.freeteam.cms.util; 2 3 import java.io.BufferedWriter; 4 import java.io.File; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.OutputStreamWriter; 8 import java.io.Writer; 9 import java.util.Locale; 10 import java.util.Map; 11 12 import javax.servlet.ServletContext; 13 14 import cn.freeteam.cms.freemarker.directive.AjaxInfoClickDirective; 15 import cn.freeteam.cms.freemarker.directive.AjaxLoadDirective; 16 import cn.freeteam.cms.freemarker.directive.AjaxStoreDirective; 17 import cn.freeteam.cms.freemarker.directive.ApplyopenQueryDirective; 18 import cn.freeteam.cms.freemarker.directive.ChannelDirective; 19 import cn.freeteam.cms.freemarker.directive.ChannelListDirective; 20 import cn.freeteam.cms.freemarker.directive.ChannelPathDirective; 21 import cn.freeteam.cms.freemarker.directive.ChannelSonDirective; 22 import cn.freeteam.cms.freemarker.directive.CommentPageDirective; 23 import cn.freeteam.cms.freemarker.directive.ConfigDirective; 24 import cn.freeteam.cms.freemarker.directive.DemoDirective; 25 import cn.freeteam.cms.freemarker.directive.DoctorInfoDirective; 26 import cn.freeteam.cms.freemarker.directive.GuestbookDirective; 27 import cn.freeteam.cms.freemarker.directive.GuestbookListDirective; 28 import cn.freeteam.cms.freemarker.directive.GuestbookPageDirective; 29 import cn.freeteam.cms.freemarker.directive.HtmlDirective; 30 import cn.freeteam.cms.freemarker.directive.InfoAttchsDirective; 31 import cn.freeteam.cms.freemarker.directive.InfoDirective; 32 import cn.freeteam.cms.freemarker.directive.InfoImgDirective; 33 import cn.freeteam.cms.freemarker.directive.InfoListDirective; 34 import cn.freeteam.cms.freemarker.directive.InfoNextListDirective; 35 import cn.freeteam.cms.freemarker.directive.InfoPageDirective; 36 import cn.freeteam.cms.freemarker.directive.InfoPreListDirective; 37 import cn.freeteam.cms.freemarker.directive.InfoRelateDirective; 38 import cn.freeteam.cms.freemarker.directive.InfoSearchDirective; 39 import cn.freeteam.cms.freemarker.directive.InfoSignDirective; 40 import cn.freeteam.cms.freemarker.directive.JobDirective; 41 import cn.freeteam.cms.freemarker.directive.JobListDirective; 42 import cn.freeteam.cms.freemarker.directive.JobPageDirective; 43 import cn.freeteam.cms.freemarker.directive.LinkClassDirective; 44 import cn.freeteam.cms.freemarker.directive.LinkDirective; 45 import cn.freeteam.cms.freemarker.directive.MailListDirective; 46 import cn.freeteam.cms.freemarker.directive.MailPageDirective; 47 import cn.freeteam.cms.freemarker.directive.MailQueryDirective; 48 import cn.freeteam.cms.freemarker.directive.MailSaveDirective; 49 import cn.freeteam.cms.freemarker.directive.QuestionListDirective; 50 import cn.freeteam.cms.freemarker.directive.QuestionOneDirective; 51 import cn.freeteam.cms.freemarker.directive.QuestionPageDirective; 52 import cn.freeteam.cms.freemarker.directive.ReportQueryDirective; 53 import cn.freeteam.cms.freemarker.directive.URLDecoderDirective; 54 import cn.freeteam.cms.freemarker.directive.URLEncoderDirective; 55 import cn.freeteam.cms.freemarker.directive.UnitListDirective; 56 import cn.freeteam.cms.freemarker.directive.UserListDirective; 57 import cn.freeteam.cms.freemarker.directive.VideoDirective; 58 import cn.freeteam.cms.freemarker.directive.VisitDirective; 59 import cn.freeteam.cms.freemarker.method.IsChildChannelMethod; 60 import freemarker.template.Configuration; 61 import freemarker.template.Template; 62 import freemarker.template.TemplateException; 63 import freemarker.template.TemplateModelException; 64 65 /** 66 * 67 * <p>Title: FreeMarkerUtil.java</p> 68 * 69 * <p>Description: freemarker工具类</p> 70 * 71 * <p>Date: Mar 9, 2012</p> 72 * 73 * <p>Time: 4:00:48 PM</p> 74 * 75 * <p>Copyright: 2012</p> 76 * 77 * <p>Company: freeteam</p> 78 * 79 * @author freeteam 80 * @version 1.0 81 * 82 * <p>============================================</p> 83 * <p>Modification History 84 * <p>Mender: </p> 85 * <p>Date: </p> 86 * <p>Reason: </p> 87 * <p>============================================</p> 88 */ 89 public class FreeMarkerUtil { 90 91 /** 92 * 生成静态页面主方法 默认编码为UTF-8 93 * @param context ServletContext 94 * @param data 一个Map的数据结果集 95 * @param templatePath ftl模版路径 96 * @param htmlPath 生成静态页面的路径 97 * @throws freemarker.template.TemplateException 98 * @throws IOException 99 */ 100 public static void createHTML(ServletContext context,Map<String,Object> data,String templatePath,String htmlPath) throws IOException, TemplateException{ 101 createHTML(context, data, "UTF-8", templatePath, "UTF-8", htmlPath); 102 } 103 /** 104 * 生成静态页面主方法 105 * @param context ServletContext 106 * @param data 一个Map的数据结果集 107 * @param templatePath ftl模版路径 108 * @param templateEncode ftl模版编码 109 * @param htmlPath 生成静态页面的路径 110 * @param htmlEncode 生成静态页面的编码 111 * @throws IOException 112 * @throws freemarker.template.TemplateException 113 */ 114 public static void createHTML(ServletContext context,Map<String,Object> data,String templetEncode,String templatePath,String htmlEncode,String htmlPath) throws IOException, TemplateException{ 115 116 Configuration freemarkerCfg=initCfg(context, templetEncode); 117 118 //指定模版路径 119 Template template = freemarkerCfg.getTemplate(templatePath,templetEncode); 120 template.setEncoding(templetEncode); 121 //静态页面路径 122 File htmlFile = new File(htmlPath); 123 if (!htmlFile.getParentFile().exists()) { 124 htmlFile.getParentFile().mkdirs(); 125 } 126 Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile), htmlEncode)); 127 //处理模版 128 template.process(data, writer); 129 writer.flush(); 130 writer.close(); 131 } 132 /** 133 * 处理页面后,装处理结果放入指定Out 134 * @param context 135 * @param data 136 * @param templatePath 137 * @throws freemarker.template.TemplateModelException 138 */ 139 public static void createWriter(ServletContext context,Map<String,Object> data,String templatePath,Writer writer) throws TemplateModelException{ 140 createWriter(context, data, "UTF-8", templatePath, "UTF-8",writer); 141 } 142 public static void createWriter(ServletContext context,Map<String,Object> data,String templetEncode,String templatePath,String htmlEncode,Writer writer) throws TemplateModelException{ 143 144 Configuration freemarkerCfg=initCfg(context, templetEncode); 145 146 try { 147 //指定模版路径 148 Template template = freemarkerCfg.getTemplate(templatePath,templetEncode); 149 template.setEncoding(templetEncode); 150 //处理模版 151 template.process(data, writer); 152 } catch (Exception e) { 153 e.printStackTrace(); 154 } 155 } 156 /** 157 * 初始化freemarker配置 158 * @return 159 * @throws freemarker.template.TemplateModelException 160 */ 161 public static Configuration initCfg(ServletContext context,String templetEncode) throws TemplateModelException{ 162 163 Configuration freemarkerCfg=null; 164 //判断context中是否有freemarkerCfg属性 165 if (context.getAttribute("freemarkerCfg")!=null) { 166 freemarkerCfg=(Configuration)context.getAttribute("freemarkerCfg"); 167 }else { 168 freemarkerCfg = new Configuration(); 169 //加载模版 170 freemarkerCfg.setServletContextForTemplateLoading(context, "/"); 171 freemarkerCfg.setEncoding(Locale.getDefault(), templetEncode); 172 173 //加载自定义标签 174 //栏目类 175 freemarkerCfg.setSharedVariable("channel", new ChannelDirective()); 176 freemarkerCfg.setSharedVariable("channelList", new ChannelListDirective()); 177 freemarkerCfg.setSharedVariable("channelSon", new ChannelSonDirective()); 178 freemarkerCfg.setSharedVariable("channelPath", new ChannelPathDirective()); 179 //信息类 180 freemarkerCfg.setSharedVariable("infoList", new InfoListDirective()); 181 freemarkerCfg.setSharedVariable("infoPreList", new InfoPreListDirective()); 182 freemarkerCfg.setSharedVariable("infoNextList", new InfoNextListDirective()); 183 freemarkerCfg.setSharedVariable("infoPage", new InfoPageDirective()); 184 freemarkerCfg.setSharedVariable("infoAttchs", new InfoAttchsDirective()); 185 freemarkerCfg.setSharedVariable("infoSearch", new InfoSearchDirective()); 186 freemarkerCfg.setSharedVariable("infoSign", new InfoSignDirective()); 187 freemarkerCfg.setSharedVariable("infoImg", new InfoImgDirective()); 188 freemarkerCfg.setSharedVariable("infoRelate", new InfoRelateDirective()); 189 freemarkerCfg.setSharedVariable("info", new InfoDirective()); 190 //访问 191 freemarkerCfg.setSharedVariable("visit", new VisitDirective()); 192 //链接类 193 freemarkerCfg.setSharedVariable("linkClass", new LinkClassDirective()); 194 freemarkerCfg.setSharedVariable("link", new LinkDirective()); 195 //Ajax类 196 freemarkerCfg.setSharedVariable("ajaxInfoClick", new AjaxInfoClickDirective()); 197 freemarkerCfg.setSharedVariable("ajaxLoad", new AjaxLoadDirective()); 198 freemarkerCfg.setSharedVariable("ajaxStore", new AjaxStoreDirective()); 199 //单位类 200 freemarkerCfg.setSharedVariable("unitList", new UnitListDirective()); 201 //用户类 202 freemarkerCfg.setSharedVariable("userList", new UserListDirective()); 203 //系统配置 204 freemarkerCfg.setSharedVariable("config", new ConfigDirective()); 205 //信件 206 freemarkerCfg.setSharedVariable("mailSave", new MailSaveDirective()); 207 freemarkerCfg.setSharedVariable("mailQuery", new MailQueryDirective()); 208 freemarkerCfg.setSharedVariable("mailList", new MailListDirective()); 209 freemarkerCfg.setSharedVariable("mailPage", new MailPageDirective()); 210 //在线申报 211 freemarkerCfg.setSharedVariable("reportQuery", new ReportQueryDirective()); 212 //依申请公开 213 freemarkerCfg.setSharedVariable("applyopenQuery", new ApplyopenQueryDirective()); 214 //网上调查 215 freemarkerCfg.setSharedVariable("questionOne", new QuestionOneDirective()); 216 freemarkerCfg.setSharedVariable("questionList", new QuestionListDirective()); 217 freemarkerCfg.setSharedVariable("questionPage", new QuestionPageDirective()); 218 //评论 219 freemarkerCfg.setSharedVariable("commentPage", new CommentPageDirective()); 220 //留言 221 freemarkerCfg.setSharedVariable("guestbookList", new GuestbookListDirective()); 222 freemarkerCfg.setSharedVariable("guestbookPage", new GuestbookPageDirective()); 223 freemarkerCfg.setSharedVariable("guestbook", new GuestbookDirective()); 224 //职位 225 freemarkerCfg.setSharedVariable("jobList", new JobListDirective()); 226 freemarkerCfg.setSharedVariable("jobPage", new JobPageDirective()); 227 freemarkerCfg.setSharedVariable("job", new JobDirective()); 228 //其它 229 freemarkerCfg.setSharedVariable("video", new VideoDirective()); 230 freemarkerCfg.setSharedVariable("URLEncoder", new URLEncoderDirective()); 231 freemarkerCfg.setSharedVariable("URLDecoder", new URLDecoderDirective()); 232 freemarkerCfg.setSharedVariable("html", new HtmlDirective()); 233 234 //pxpt自定义标签 235 freemarkerCfg.setSharedVariable("doctorInfo", new DoctorInfoDirective()); 236 237 freemarkerCfg.setSharedVariable("demo", new DemoDirective()); 238 239 //pxpt自定义method 240 freemarkerCfg.setSharedVariable("isChildOf", new IsChildChannelMethod()); 241 } 242 return freemarkerCfg; 243 } 244 }