Apache Velocity简介

开发者文档:http://velocity.apache.org/engine/devel/developer-guide.html
http://velocity.apache.org/engine/devel/user-guide.html
模板语言:http://velocity.apache.org/engine/devel/vtl-reference-guide.html
1、Velocity如何工作:
(1)、初始化Velocity,一个应用可以创建一个Singleton对象Velocity或者为每一个实现创建VelocityEngine对象;Initialize Velocity.
(2)、创建一个Context对象(VelocityContext);Create a Context object
(3)、把数据对象添加到Context中;Add your data objects to the Context.
(4)、选择一个模板;Choose a template.
(5)、合并模板与数据产生输出流。'Merge' the template and your data to produce the ouput.

2、单例模式:org.apache.velocity.app.Velocity
import  java.io.StringWriter;

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

Velocity.init();

VelocityContext context =  new  VelocityContext();
context.put( "name" ,  new  String( "Velocity"  ));

Template template =  null ;

try  {
    template = Velocity.getTemplate(  "mytemplate.vm" );
}  catch  (ResourceNotFoundException rnfe) {
     // couldn't find the template
}  catch  (ParseErrorException pee) {
     // syntax error: problem parsing the template
}  catch  (MethodInvocationException mie) {
     // something invoked in the template
     // threw an exception
}  catch  (Exception e) {
}

StringWriter sw =  new  StringWriter();
template.merge(context, sw);

3、非单例模式:org.apache.velocity.app.VelocityEngine
VelocityEngine velocityEngine =  new  VelocityEngine();
velocityEngine.init();

VelocityContext context =  new  VelocityContext();
context.put( "name" ,  new  String( "Velocity"  ));

Template template =  null ;

try  {
    template = velocityEngine.getTemplate(  "mytemplate.vm" );
}  catch  (ResourceNotFoundException rnfe) {
     // couldn't find the template
}  catch  (ParseErrorException pee) {
     // syntax error: problem parsing the template
}  catch  (MethodInvocationException mie) {
     // something invoked in the template
     // threw an exception
}  catch  (Exception e) {
}

StringWriter sw =  new  StringWriter();
template.merge(context, sw);

4、Context:VelocitContext
VelocityContext提供两个最主要的方法:
public  Object put(String key, Object value);
public  Object get(String key);

(1)、使用#foreach()支持迭代对象; Support for Iterative Objects for #foreach()
     支持类型:Object[], java.util.Collection,java.util.Map,java.util.Iterator,java.util.Enumeration
(2)、支持类对象。 Support for "Static Classes"
     context.put( "Math" , Math.  class );
     在模板中 $Math 以使用 java.lang.Math静态方法

5、使用Velocity
Velocity有5个方法:
  • setProperty(String key, Object o):设置key的值
  • Object getProperty(String key):根据key获取值
  • init():使用默认的properties配置文件初始化
  • init(Properties p):根据Properties对象设置参数初始化
  • init(String filename):根据properties文件初始化
认properties配置文件所在位置:org/apache/velocity/runtime/defaults/velocity.properties,相关配置见官方文档
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值