一:velocity模版引擎
地址:http://velocity.apache.org/
内容很详细,可以仔细看看。
二:简单介绍
1:写一个后缀为vm的文件,里面可以任意写内容,把要替换的内容换成占位符,按照规则。
2:调用java代码,替换相应的内容,生成相应的文件等类型的输出,按照规则。
三:实现目的
提供as 变量名称和变量类型,生成as的get/set方法
四:例子
1:运行main方法,提供
public static void main(String[] args) {
String property = "hostMessage";
String type = "String";
Auto.autoMethod(property,type);
}
2:输出
private var _hostMessage:String;
public function get hostMessage():String{
return this._hostMessage;
}
public function set hostMessage(hostMessage:String):void{
this._hostMessage = hostMessage;
}
五:code
1:vm文件
private var _${property}:${type};
public function get ${property}():${type}{
return this._${property};
}
public function set ${property}(${property}:${type}):void{
this._${property} = ${property};
}
2::java文件
public class Auto {
/**
* 自动生成as的get/set方法
* @param property
* @return
*/
public static String autoMethod(String property,String type){
VelocityEngine ve = new VelocityEngine();
ve.setProperty(Velocity.RESOURCE_LOADER, "class");
ve.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
ve.init();
/* next, get the Template */
Template t = ve.getTemplate("method.vm");
/* create a context and add data */
VelocityContext context = new VelocityContext();
context.put("property", property);
context.put("type", type);
/* now render the template into a StringWriter */
StringWriter writer = new StringWriter();
t.merge(context, writer);
/* show the World */
String ret = writer.toString();
System.out.println(ret);
return ret;
}
public static String autoBean(){
return null;
}
public static void main(String[] args) {
String property = "hostMessage";
String type = "String";
Auto.autoMethod(property,type);
}
}
注:需要加载velocity jar包和velocity的依赖jar包,下载velocity,在lib中可以找到