Apache Velocity简介
来源:互联网 发布:球心检测算法 编辑:程序博客网 时间:2024/06/06 03:07
开发者文档: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
- Apache Velocity简介
- org.apache.velocity 简介
- org.apache.velocity 简介
- Velocity简介
- Velocity简介
- Velocity简介
- Velocity简介
- Velocity简介
- Velocity简介
- Apache 系列:Velocity HelloWorld
- The Apache Velocity Project
- The Apache Velocity Project
- velocity模板引擎简介
- 模板语言:Velocity简介
- velocity的简介
- velocity的使用简介
- Velocity org.apache.velocity.exception.ParseErrorException
- Apache Velocity实现模板化
- swing技术交流群373204858
- 解决Myeclipse中logcat不显示log的问题
- 如何查看网段内正在使用的IP以及ip定位
- linux下多网卡路由设置
- html5 CSS兼容相关记录
- Apache Velocity简介
- Cocos2d-X Json格式文件的解析
- Linux下gsoap使用说明
- 分布式版本管理工具GIT使用心得
- Hadoop初步使用:通过web了解Hadoop的活动
- Hadoop在Linux下伪分布式的安装 wordcount实例的运行
- POJ 2115--C Looooops
- OC学习总结
- 无焦点窗口创建(转)