Velocity心得

本文介绍Velocity模板引擎的基本使用方法,包括设置文件路径、展示JavaBean、处理空值及列表显示等,并提供示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


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

/**//*
*CreatedonDec1,2006
*
*Tochangethetemplateforthisgeneratedfilegoto
*Window&gt;Preferences&gt;Java&gt;CodeGeneration&gt;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&gt;Preferences&gt;Java&gt;CodeGeneration&gt;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));


}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值