作为CMS内容发布来说,将要显示的内容html化无疑是一个很基本的要求,这样可以提高整个系统的效率。
考虑一个内容节点Content,其中节点有个pagecontent的属性,这个属性用来存储用户在后台输入的内容,
内容+模板=显示,这里是:
内容+模板-->显示静态html
内容用FCK来编辑,模板也同样用FCK编辑,这里用freemarker无疑是一个好的选择。这里是个最简单的例子。
首先定义一个最简单的模板content.ftl
<html> ${content} </html>
然后就是把内容填充进去:
其实就是这么简单!对html的管理就很容易了,基本的FILE操作
Configuration cfg = new Configuration(); //模板存储的目录 cfg.setDirectoryForTemplateLoading(Path.getTemplateDirectory()); //默认 cfg.setObjectWrapper(new DefaultObjectWrapper()); //填充数据 Map root = new HashMap(); root.put("content", c.getPageContent()); Template temp = cfg.getTemplate("content.ftl"); //发布日期 String date=c.getActivedate(); String filename = c.getId()+".html"; //创建目录 Path.createFold(Path.getWebCmsHtmlDirPath(),date); Writer out = new OutputStreamWriter(new FileOutputStream(Path.getWebCmsHtmlDirPath()+"/"+date+"/"+filename)); temp.process(root, out); out.flush();