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();
  }
 }

 

:模板文件((_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> 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值