您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

velocity第五个应用例子---获得当前迭代的索引

2025/12/1 4:28:16发布53次查看
velocity第五个应用例子---获得当前迭代的索引
#foreach($key in $map.keyset()) $velocitycount > $key : $map.get($key) #end $velocitycount获得当前迭代索引 velocitycount变量名可以通过directive.foreach.counter.name属性修改, 如:directive.foreach.counter.name=index,以后可以通过$index进行访问。 迭代的索引默认从1开始,我们可以通过directive.foreach.inital.value=0进行修改。
控台输出
1> key4 : value4 2> key3 : value3 3> key2 : value2 4> key1 : value1
修改索引变量名
在属性文件增加配置
#修改索引变量名 directive.foreach.counter.name=index index.vm #foreach($key in $map.keyset()) $index >>> $key : $map.get($key) #end #修改迭代索引的默认值 directive.foreach.counter.initial.value=0
模板
#foreach($key in $map.keyset()) $index >>> $key : $map.get($key) #end
控台输出
0 >>> key4 : value4 1 >>> key3 : value3 2 >>> key2 : value2 3 >>> key1 : value1 //2 create acontext object velocitycontextcontext = new velocitycontext(); //3 add you dataobjects to this context map<string,string>map = new hashmap<string,string>(); map.put("key1","value1"); map.put("key2","value2"); map.put("key3","value3"); map.put("key4","value4"); context.put("map",map); //4 choose atemplate templatetemplate = velocity.gettemplate("index.vm"); //5 merge thetemplate and you data to produce the output stringwriter sw= new stringwriter(); template.merge(context,sw); sw.flush(); system.out.println(sw.tostring());
模板
#foreach($keyin $map.keyset()) $velocitycount > $key : $map.get($key) #end ============= #foreach($keyin $map.keyset()) $index >>> $key :$map.get($key) #end
以上就是velocity第五个应用例子---获得当前迭代的索引的内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product