//2.create a context object velocitycontext context = newvelocitycontext(); //3.add your data objects to this context context.put("list",arrays.aslist("第一个","第二个","第三个","第四个")); //4.choose a template template template =velocity.gettemplate("list.vm"); //5.merge the template and your data toproduce the output stringwriter sw = new stringwriter(); template.merge(context, sw); sw.flush(); system.out.println(sw.tostring());
模板
#foreach($elementin $list) $element #end ====================== #foreach($ein $list) $e #end
以上就是velocity第三个应用例子--遍历集合/数组的内容。