温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

雪山飞鹄

温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

BlogJava 首页 新随笔 联系 聚合 管理
  215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
Java code

package com.velocity.test;
import java.io.IOException;
import java.io.StringWriter;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;


public class VelocityTest {

 public static void main(String[] args) {
  
  
  try {
   Velocity.init();
  } catch (Exception e) {
   e.printStackTrace();
  }
  
  VelocityEngine engine=new VelocityEngine();
  engine.setProperty(Velocity.RESOURCE_LOADER, "class");
  engine.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
  
  VelocityContext context= new VelocityContext();
  context.put("name", "林心如");
  context.put("velocity", "Velocity");
  
  Template template = null;
  try {
   template = engine.getTemplate("velocity.vm","gbk");
  } catch (ResourceNotFoundException e) {
   e.printStackTrace();
  } catch (ParseErrorException e) {
   e.printStackTrace();
  } catch (Exception e) {
   e.printStackTrace();
  }
  
  StringWriter writer=new StringWriter();
  
  try {
   template.merge(context, writer);
   System.out.println(writer.toString());
  } catch (ResourceNotFoundException e) {
   e.printStackTrace();
  } catch (ParseErrorException e) {
   e.printStackTrace();
  } catch (MethodInvocationException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }

}


Velocity 模版
Hello $name ,
 this is my first $velocity !!!
#set($sex="女")
*******************************************
$sex
#**
 #set($a=true)
 
 #if($a)
  true
 #else
  false
 #end
 
 #set()
*#

Jar包支持
avalon-logkit-2.1.jar
commons-collections-3.2.1.jar
commons-lang-2.4.jar
oro-2.0.8.jar
velocity-1.6.3.jar
posted on 2010-02-23 10:36 雪山飞鹄 阅读(2390) 评论(1)  编辑  收藏 所属分类: velocity

Feedback

# re: First Velocity Demo 2014-09-15 22:10 zuidaima
velocity demo教程源代码下载:http://zuidaima.com/share/kvelocity-p1-s1.htm  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: