velocity 生成html

来源:互联网 发布:绿色上网软件 编辑:程序博客网 时间:2024/04/30 13:58

还是很简单的demo

用来处理从后台文本编辑器中传来的内容,比如在做网站的时候可以将新闻这些做成静态页面,然后访问这些静态页面

提高访问的速度


public String addNews() throws Exception{ActionContext ac = ActionContext.getContext();Properties p = new Properties();p.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");VelocityEngine ve = new VelocityEngine();ve.init(p);Template t = ve.getTemplate("hellovelocity.vm","utf-8");VelocityContext vc = new VelocityContext();vc.put("content", content);HttpServletResponse response = ServletActionContext.getResponse();response.setContentType("text/html;charset=gb2312");long time = new Date().getTime();PrintWriter pw = new PrintWriter(ServletActionContext.getServletContext().getRealPath("/news/"+time+".html"));t.merge(vc, pw);pw.close();ac.put("html", time+".html");return SUCCESS;}



原创粉丝点击