Velocity心得

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目录下的文件。
一个例子如下:
public class VelocityTest
... {


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)
< font face ="Arial,Helvetica,sans-serif" > $!element.pendacca </ font >
#end
3:如果某值没有设置,或者为空,可以在$后加!,写成类似 $!var 的形似,否则页面显示出来的就是“$var”。
4:关于list和hashmap的显示:
ListmapList = new ArrayList();
MaphashMap
= new HashMap();
hashMap.put(
" NARR " , " myNarrinhashMap " );
mapList.add(hashMap);
context.put(
" SUB " ,mapList);

此时,如果要列出hashMap中key=“NARR”的值,可以如下:
#foreach($elementin$SUB)
< font face ="Arial,Helvetica,sans-serif" > $!element.get("NARR") </ font >
#end

如果要全部列出hashmap的key-value,可以如下:
#foreach($keyin$hashVariable.keySet())
< li > $key‘svalue:$hashVariable.get($key) </ li >
#end


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

/**/ /*
*CreatedonDec1,2006
*
*Tochangethetemplateforthisgeneratedfilegoto
*Window&gt;Preferences&gt;Java&gt;CodeGeneration&gt;CodeandComments
*/

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;
/***/ /**
*
@authorLichunlei
*
*Tochangethetemplateforthisgeneratedtypecommentgoto
*Window&gt;Preferences&gt;Java&gt;CodeGeneration&gt;CodeandComments
*/

public class VelocityBase
... {
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;
}


}

用法如下:

public class VelocityAsonTest
... {

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));


}


}

你可能感兴趣的:(apache,spring,bean,velocity,OS)