当前位置:   article > 正文

Apache Velocity简介_apache velocitycontext

apache velocitycontext
开发者文档: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,相关配置见官方文档
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/58689
推荐阅读
相关标签
  

闽ICP备14008679号