使用注解生成代码
五、Velocity的历史简介
Velocity,Apache软件基金会的一个项目,是一个用Java写的模板引擎,用于将模板和从Java对象中获取的数据进行混合生成各种文字类型的文件。
Velocity经常在当下流行的MVC模式中被用来渲染视图,或者在XML文件中作为XSLT的替代品进行数据转换。
Velocity拥有自己的语言,也就是Velocity Template Language(VLT),它是生成简单易读模板的关键。使用VLT,我们可以简单且直观地定义变量,控制流程和迭代,以及访问Java对象中包含的信息。
下面是一段Velocity模板片段:
#foreach($field in $fields)
/**
* Returns the ${field.simpleName} property descriptor.
*
* @return the property descriptor
*/
public PropertyDescriptor ${field.simpleName}PropertyDescriptor() {
PropertyDescriptor theDescriptor = null;
return theDescriptor;
}
#end
#foreach($method in $methods)
/**
* Returns the ${method.simpleName}() method descriptor.
*
* @return the method descriptor
*/
public MethodDescriptor ${method.simpleName}MethodDescriptor() {
MethodDescriptor descriptor = null;
return descriptor;
}
#end