java里生成HTML的静态文件也是经常要用到的,今小结之,这里用的是spring mvc,其他的框架都差不都的思路.
1 新闻模版,用freemarker实现. <html>< head>< title>${news.newstitle}</title>< /head>< body>< table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td align="center"><table width="778" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td> <table cellspacing="0" cellpadding="0" width="100%"> <tr> <td class="news_title" align="center" bgcolor="#F8F8F8">${news.newstitle}</td> </tr> <tr> <td height="1" bgcolor="#DDDDDD"></td> </tr> <tr> <td height="30" align="center" bgcolor="#F8F8F8"><font color="#FF3300">${news.newsfrom}</font> ${news.newsdate} <font color="#0000FF">${news.newsauthor}</font></td> </tr> <tr> <td valign="top" class="p_content" bgcolor="#F8F8F8">${news.newscontent}</td> </tr> </table> </td> </tr> </table></td> </tr>< /table>< /body>< /html>
2 我们考察其dao层的insert 方法如下 public void insertNews(News news) throws DataAccessException {TemplateParam templateParam = SetTemplateParam(news);// 生成静态新闻news.setFilename(new MakeFile().make(news, templateParam));this.getHibernateTemplate().save(news);
/** * 修改新闻 * * @param news * */public void updateNews(News news) throws DataAccessException {TemplateParam templateParam = SetTemplateParam(news);// 更新原来的静态新闻new MakeFile().update(news, templateParam);this.getHibernateTemplate().update(news);}
private TemplateParam SetTemplateParam(News news) {// 设置模板参数TemplateParam templateParam = new TemplateParam();templateParam.setRealPath(news.getRealPath());templateParam.setTemplateName("news.ftl");return templateParam;}
这个TemplateParam类的定义如下,主要是保存每个一些其他附加属性,比如模版名,模版所在的路径,新闻要保存的目录,
public class TemplateParam {private String realPath;
private String templatePath;
private String saveDirectory;
private String filePostfix;private String templateName;
之后,public void insertNews(News news) throws DataAccessException {TemplateParam templateParam = SetTemplateParam(news);// 生成静态新闻news.setFilename(new MakeFile().make(news, templateParam)); 中,先调用makefile()方法,把生成的静态文件先保存到磁盘上,然后在用DAO入库news实体的其他属性.
再看makefile函数
import freemarker.template.Configuration;import freemarker.template.DefaultObjectWrapper;import freemarker.template.Template;import freemarker.template.TemplateException;
public String make(News news, TemplateParam templateParam) {Configuration cfg = new Configuration();// 项目真实路径String realPath = templateParam.getRealPath();// 新闻模板路径String templatePath = realPath + Constant.NEWSTEMPLATEPATH;
//存放在news目录下的以yyyymmdd表示的子目录中,即按天数来存放String cDateStr = "news/" + PureUtil.getDateFormatStr("yyyyMMdd");
//文件后缀String filePostfix = ".html";
// 静态新闻生成文件存放路径String path = realPath + cDateStr;
String fileTimeName = PureUtil.getDateFormatStr("yyyyMMddhhmmss");// 写到数据库地路径String returnFileName = cDateStr + "/" + fileTimeName + filePostfix;String fileName = "";File newsDir = new File(path);fileName = path + "/" + fileTimeName + filePostfix;if (!newsDir.exists()) {newsDir.mkdirs();fileName = path + "/" + fileTimeName + filePostfix;}try {cfg.setDirectoryForTemplateLoading(new File(templatePath));cfg.setObjectWrapper(new DefaultObjectWrapper());Template newsTemplate = cfg.getTemplate(templateParam.getTemplateName());newsTemplate.setEncoding("GBK");
Map root = new HashMap();root.put("news", news);Writer out = new OutputStreamWriter(new FileOutputStream(fileName));try {newsTemplate.process(root, out);} catch (TemplateException e) {e.printStackTrace();}out.flush();out.close();} catch (IOException e) {e.printStackTrace();}return returnFileName;}
/** * 重新生成html文件,使用原来的文件名 * * @param news * @param templateParam */public void update(News news, TemplateParam templateParam) {Configuration cfg = new Configuration();// 项目真实路径String realPath = templateParam.getRealPath();// 新闻模板路径String templatePath = realPath + Constant.NEWSTEMPLATEPATH;String fileName = news.getFilename();// 原来的路径,当删除原来的目录时,重建目录String path = realPath+ fileName.substring(0, fileName.lastIndexOf("/"));fileName = realPath + news.getFilename();File oldDir = new File(path);if (!oldDir.exists()) {oldDir.mkdirs();}
//注意下面是用freemarker来处理的了.
try {cfg.setDirectoryForTemplateLoading(new File(templatePath));cfg.setObjectWrapper(new DefaultObjectWrapper());Template newsTemplate = cfg.getTemplate(templateParam.getTemplateName());newsTemplate.setEncoding("GBK");Map root = new HashMap();
//freemarker中用news对象放到map里面去.root.put("news", news);Writer out = new OutputStreamWriter(new FileOutputStream(fileName));try {newsTemplate.process(root, out);} catch (TemplateException e) {e.printStackTrace();}out.flush();out.close();} catch (IOException e) {e.printStackTrace();}}
幸福就是重复。每天跟自己喜欢的人一起,通电话,