Velocity是一种Java模版引擎技术,该项目由Apache提出,功能强大。用了一段时间,有点心得,总结如下:
1:装载vm模版时,需要设置Velocity.FILE_RESOURCE_LOADER_PATH属性,
String loadpath;
Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, loadpath);
此loadpath为vm文件的文件夹,是绝对路径。
而 Velocity.getTemplate(String filename)中,filename是loadpath目录下的文件。
一个例子如下:
public
class
VelocityTest

...
{


public static void main(String[] args)

...{

try

...{
String loadpath = FileUtil.getPackageResourcePath("META-INF/profile/vm");
Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, loadpath);

Velocity.init();
VelocityContext context = new VelocityContext();
context.put( "TN", new String("999111888") );
List FLAG_FIVE = new ArrayList();
FLAG_FIVE.add("MYSTART1");
FLAG_FIVE.add("MYSTART2");
FLAG_FIVE.add("MYSTART3");
context.put("FLAG_FIVE", FLAG_FIVE);
List FLAG = new ArrayList();
FLAG.add("-FLAG1");
FLAG.add("-FLAG2");
FLAG.add("-FLAG3");
context.put("FLAG", FLAG);
List CAF = new ArrayList();
CAF.add("mycaf");
context.put("CAF", CAF);
SubBean subBean = new SubBean();
subBean.setNarr("myNARR");
subBean.setAccb("myACCB");
subBean.setHndl("myHNDL");
subBean.setOldcomm("myOLDCOMM");
subBean.setPendacca("myPENDACCA");
subBean.setSub_cnt("mySUB_CNT");
List subList = new ArrayList();
subList.add(subBean);
context.put("SUB",subList);
context.put("BC","myBC");
context.put("AS","myAS");
context.put("OS","myOS");
Template template = Velocity.getTemplate("osstr.vm");
StringWriter sw = new StringWriter();
template.merge( context, sw );
System.out.println(sw.toString());
}
catch( ResourceNotFoundException rnfe )

...{
rnfe.printStackTrace();
System.out.println("couldn't find the template");
}
catch( ParseErrorException pee )

...{
System.out.println("syntax error : problem parsing the template");
}
catch( MethodInvocationException mie )

...{
System.out.println("something invoked in the template threw an exception");
}
catch( Exception e )

...{
System.out.println("Unknown Exception!");
}


}
}
2:在页面上展现一个javabean,该bean一定要是public的,不能使内部类。此问题让我浪费了1个小时。比如上面的代码中subBean一定不能使内部类。否则无法显示。
下面是显示列表中的javabean的vlt语法:
#foreach($element in $SUB)
<
font
face
="Arial, Helvetica, sans-serif"
>
$!element.pendacca
</
font
>
#end
3:如果某值没有设置,或者为空,可以在$后加!,写成类似 $!var 的形似,否则页面显示出来的就是“$var”。
4:关于list和hashmap的显示:
List mapList
=
new
ArrayList();
Map hashMap
=
new
HashMap();
hashMap.put(
"
NARR
"
,
"
myNarr in hashMap
"
);
mapList.add(hashMap);
context.put(
"
SUB
"
, mapList);
此时,如果要列出hashMap中key=“NARR”的值,可以如下:
#foreach($element in $SUB)
<
font
face
="Arial, Helvetica, sans-serif"
>
$!element.get("NARR")
</
font
>
#end
如果要全部列出hashmap的key-value,可以如下:
#foreach($key in $hashVariable.keySet() )
<
li
>
$key ‘s value: $ hashVariable.get($key)
</
li
>
#end
/**/
/*
* Created on Dec 1, 2006
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package
co.test;

import
java.io.StringWriter;
import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.List;
import
java.util.Map;

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

/** */
/**
* @author Lichunlei
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public
class
VelocityBase

...
{
private String loadpath;
private String templatefile;
public VelocityBase(String loadpath,String templatefile)

...{
this.templatefile = templatefile;
this.loadpath = loadpath;
}
public String getResult(VelocityContext context)

...{
String result = null;
try

...{
Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, loadpath);
Velocity.init();
Template template = Velocity.getTemplate(this.templatefile);
StringWriter sw = new StringWriter();
template.merge( context, sw );
result = sw.toString();
}
catch (Exception e)

...{
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}

}
public
class
VelocityAsonTest

...
{

public static void main(String[] args)

...{

String loadpath = FileUtil.getPackageResourcePath("META-INF/profile/vm");
String templatefile = "ason.vm";
VelocityBase velocity = new VelocityBase(loadpath, templatefile);
VelocityContext context = new VelocityContext();
context.put( "TN", new String("999999999") );
System.out.println(velocity.getResult(context));


}

}
1:装载vm模版时,需要设置Velocity.FILE_RESOURCE_LOADER_PATH属性,


而 Velocity.getTemplate(String filename)中,filename是loadpath目录下的文件。
一个例子如下:


















































































下面是显示列表中的javabean的vlt语法:



4:关于list和hashmap的显示:





此时,如果要列出hashMap中key=“NARR”的值,可以如下:



如果要全部列出hashmap的key-value,可以如下:



5:spring框架已经提供了对velocity的支持,在视图方面可以取代jsp。具体配置,可以参考“spring in action”
6:备份一个用于测试的class:




































































用法如下:




















