velocity1.5使用总结

1:下载地址:http://velocity.apache.org/download.cgi(velocity-dep-1.5.jar)

 

2:velocity初始化(最好在tomcat启动时执行):

 private void initVelocity() throws Exception {
  Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH,
    getServletContext().getRealPath("c:/template"));
  Velocity.setProperty(Velocity.COUNTER_NAME, "velocityCount");
  Velocity.setProperty(Velocity.COUNTER_INITIAL_VALUE, "0");
  Velocity.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
  Velocity.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
  Velocity.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
  Velocity.init();
 }

 

3:存放velocity信息的javabean(VelocityInfo)

package javabean;

import org.apache.velocity.VelocityContext;


public class VelocityInfo {
 
 private String templateName;//模板的真实路径
 private String htmlName;//静态化文件的真实路径
 private VelocityContext context;//标记语言的数据
 
 public String getHtmlName() {
  return htmlName;
 }
 public void setHtmlName(String htmlName) {
  this.htmlName = htmlName;
 }
 public String getTemplateName() {
  return templateName;
 }
 public void setTemplateName(String templateName) {
  this.templateName = templateName;
 }
 public VelocityContext getContext() {
  return context;
 }
 public void setContext(VelocityContext context) {
  this.context = context;
 }

}

 

 4.得到要预览文件的数据
 protected VelocityInfo getPrevieArticleData(Article article) {
  VelocityInfo velocityInfo = new VelocityInfo();
  velocityInfo.setTemplateName(article.getColumnPath() + "_article.vm");

  VelocityContext context = new VelocityContext();
       context.put("publicTime", "2008-10-01");
       context.put("title", "测试文章");
       context.put("content", "测试数据。。。");
       context.put("webRoot", "http://127.0.0.1");
       velocityInfo.setContext(context);

  velocityInfo.setContext(context);

  return velocityInfo;
 }

 

5:得到要静态化文件的数据
 protected VelocityInfo getStaticArticleData(Article article) {
  VelocityInfo velocityInfo = getPrevieArticleData(article);
  String htmlPath = InitServlet.PATH + "/" +  article.getChannelPath() + "/"
     + article.getColumnPath() + "/" + PubFun.getDateTime("yyyy-MM-dd", article.getCreateTime());
  File f = new File(htmlPath);
  if (!f.exists()) {
    f.mkdirs();
   }
  velocityInfo.setHtmlName(htmlPath + "/" + article.getId() + ".html");

  return velocityInfo;
 }

 

6:预览volecity页面

private void velocityPreview(VelocityInfo velocityInfo, HttpServletResponse resp) throws Exception{
  Template t = null;
  PrintWriter out = null;
  try {
   t = Velocity.getTemplate(velocityInfo.getTemplateName());
   out = resp.getWriter();
   t.merge(velocityInfo.getContext(), out);
  } catch (ParseErrorException e) {
   throw new Exception("模板格式不对:" + e);
  } finally {
   if (out != null)
    out.close();
  }

 

7:静态化页面

 private void velocityStatic(VelocityInfo velocityInfo) throws Exception{
  Template t = null;
  Writer out = null;
  try {//文件不存在可以自动生成;目录一定要存在
   t = Velocity.getTemplate(velocityInfo.getTemplateName());
   out = new BufferedWriter(new FileWriter(velocityInfo.getHtmlName()));
   t.merge(velocityInfo.getContext(), out);
  } catch (ParseErrorException e) {
   throw new Exception("模板格式不对:" + e);
  } finally {
   if (out != null)
    out.close();
  }
 }

 

8 :模板文件((_article.vm)例子

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
<head>
<title>测试velocity</title>
<link href="${webRoot}/css/yucheng.css" rel="stylesheet" type="text/css" />
</head>
<body
<p align="center">${title}<p>

<p align="center">${publicTime}<p>

<p align="center">${content}<p>
<p align="center"><a href="javascript:window.close();">关闭窗口</a> ]</p>
</body>
</html> 

 

你可能感兴趣的:(velocity1.5使用总结)