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

...{


publicstaticvoidmain(String[]args)

...{

try

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

Velocity.init();
VelocityContextcontext=newVelocityContext();
context.put("TN",newString("999111888"));

ListFLAG_FIVE=newArrayList();
FLAG_FIVE.add("MYSTART1");
FLAG_FIVE.add("MYSTART2");
FLAG_FIVE.add("MYSTART3");
context.put("FLAG_FIVE",FLAG_FIVE);

ListFLAG=newArrayList();
FLAG.add("-FLAG1");
FLAG.add("-FLAG2");
FLAG.add("-FLAG3");
context.put("FLAG",FLAG);

ListCAF=newArrayList();
CAF.add("mycaf");
context.put("CAF",CAF);

SubBeansubBean=newSubBean();
subBean.setNarr("myNARR");
subBean.setAccb("myACCB");
subBean.setHndl("myHNDL");
subBean.setOldcomm("myOLDCOMM");
subBean.setPendacca("myPENDACCA");
subBean.setSub_cnt("mySUB_CNT");
ListsubList=newArrayList();
subList.add(subBean);
context.put("SUB",subList);

context.put("BC","myBC");
context.put("AS","myAS");
context.put("OS","myOS");


Templatetemplate=Velocity.getTemplate("osstr.vm");
StringWritersw=newStringWriter();
template.merge(context,sw);
System.out.println(sw.toString());
}
catch(ResourceNotFoundExceptionrnfe)

...{
rnfe.printStackTrace();
System.out.println("couldn'tfindthetemplate");
}
catch(ParseErrorExceptionpee)

...{
System.out.println("syntaxerror:problemparsingthetemplate");
}
catch(MethodInvocationExceptionmie)

...{
System.out.println("somethinginvokedinthetemplatethrewanexception");
}
catch(Exceptione)

...{
System.out.println("UnknownException!");
}



}
}
2:在页面上展现一个javabean,该bean一定要是public的,不能使内部类。此问题让我浪费了1个小时。比如上面的代码中subBean一定不能使内部类。否则无法显示。
下面是显示列表中的javabean的vlt语法:
#foreach($elementin$SUB)
<fontface="Arial,Helvetica,sans-serif">$!element.pendacca</font>
#end
3:如果某值没有设置,或者为空,可以在$后加!,写成类似 $!var 的形似,否则页面显示出来的就是“$var”。
4:关于list和hashmap的显示:
ListmapList=newArrayList();
MaphashMap=newHashMap();
hashMap.put("NARR","myNarrinhashMap");
mapList.add(hashMap);
context.put("SUB",mapList);
此时,如果要列出hashMap中key=“NARR”的值,可以如下:
#foreach($elementin$SUB)
<fontface="Arial,Helvetica,sans-serif">$!element.get("NARR")</font>
#end
如果要全部列出hashmap的key-value,可以如下:
#foreach($keyin$hashVariable.keySet())
<li>$key‘svalue:$hashVariable.get($key)</li>
#end

/**//*
*CreatedonDec1,2006
*
*Tochangethetemplateforthisgeneratedfilegoto
*Window>Preferences>Java>CodeGeneration>CodeandComments
*/
packageco.test;

importjava.io.StringWriter;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;

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

/***//**
*@authorLichunlei
*
*Tochangethetemplateforthisgeneratedtypecommentgoto
*Window>Preferences>Java>CodeGeneration>CodeandComments
*/
publicclassVelocityBase

...{
privateStringloadpath;
privateStringtemplatefile;

publicVelocityBase(Stringloadpath,Stringtemplatefile)

...{

this.templatefile=templatefile;
this.loadpath=loadpath;
}

publicStringgetResult(VelocityContextcontext)

...{
Stringresult=null;
try

...{
Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH,loadpath);
Velocity.init();
Templatetemplate=Velocity.getTemplate(this.templatefile);
StringWritersw=newStringWriter();
template.merge(context,sw);
result=sw.toString();
}
catch(Exceptione)

...{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}

returnresult;
}

}
publicclassVelocityAsonTest

...{

publicstaticvoidmain(String[]args)

...{

Stringloadpath=FileUtil.getPackageResourcePath("META-INF/profile/vm");
Stringtemplatefile="ason.vm";
VelocityBasevelocity=newVelocityBase(loadpath,templatefile);

VelocityContextcontext=newVelocityContext();
context.put("TN",newString("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:




































































用法如下:




















