1.velocity 获取list 中的值
VelocityEngine veloEngine = new VelocityEngine();
Template template = veloEngine.getTemplate(xmlPath, "UTF-8");
VelocityContext velocityContext = new VelocityContext();
List<Object> list = new ArrayList<Object>();
list.add("1");
list.add("2");
velocityContext.put("list", list);
XML中写法为
#foreach($data in $list)
<risk>
<age>$!data </age> @$!其中!代表非空时取值,避免值为空时显示为data
</risk>
#end
2.velocity 获取map 中的值
Map<String,Object> map = new HashMap<String,Object>();
map.put("age", "11");
map.put("sex","男");
velocityContext.put("data", map);
XML中写法为
<age>$!data.age</age>
<sex>$!data.sex</sex>
3.velocity 获取list套map 中的值
List<Object> list = new ArrayList<Object>();
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("age", "11");
Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("age", "22");
map3.put("bname", "bname");
list.add(map2);
list.add(map3);
Map<String, Object> map = new HashMap<String, Object>();
map.put("list", list);
velocityContext.put("data", map);
XML中写法为
#foreach ($lis in ${data}) @第一次循环获取list
#foreach( $aa in $lis) @然后将lis循环获取变量
<risk>
<age>$!aa.age </age>
<age>$!aa.bname </age>
</risk>
#end
#end