关键字: web
今天设计了一个功能,发邮件,而且邮件的内容是html。html内容里面带有动态内容,因此觉得freemarket是个不错的选择。
结合同事以前对Freemarket的处理,自己在同事的代码的基础上稍微修改了下,不过估计网络上相同功能的东西太多了。
java 代码
- class HtmlTemplateGenerator {
-
- Configuration cfg = null;
-
- public HtmlTemplateGenerator(String templatePath) throws IOException {
- cfg = new Configuration();
- cfg.setDefaultEncoding("UTF-8");
- cfg.setDirectoryForTemplateLoading(new File(templatePath));
- cfg.setObjectWrapper(new DefaultObjectWrapper());
- }
-
-
-
-
-
-
-
-
-
-
- public void create(String ftlTemplate, Map contents, String savePath, String saveFilename) throws IOException, TemplateException {
- Template temp = cfg.getTemplate(ftlTemplate);
-
-
- String realPath = ServletActionContext.getServletContext().getRealPath(savePath);
- System.out.println( saveFilename + ":" + realPath);
- File file = new File(realPath);
- if(!file.exists())
- file.mkdirs();
-
- Writer out = new OutputStreamWriter(new FileOutputStream(realPath + "/" + saveFilename),"UTF-8");
- temp.process(contents, out);
- out.flush();
- }
-
- public String getContentFromTemplate(String ftlTemplate, Map contents) throws IOException, TemplateException {
- Template temp = cfg.getTemplate(ftlTemplate);
-
- StringWriter sw = new StringWriter();
- temp.process(contents, sw);
- return sw.toString();
- }
-
这样我们就可以通过
getContentFromTemplate()获取html内容,然后再发送这个内容到对方的邮件中,
至于模板码,可以像写普通的freemarket文件那么简单。发表于 @ 2007年04月22日 15:05:00|评论(loading...)|编辑