1 优点
- 比BeanUtils有更多的操作属性的能力,比如操作List[index]属性
2 实践
- 引入jar包
<dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>3.2</version>
</dependency>
- get & set
HashMap map = new HashMap();
List list= new ArrayList();
list.add("a");
list.add("b");
list.add("c");
map.put("list", list);
// getValue, p1 => ognl expression, p2 => root element, r => b
Object r = Ognl.getValue("list[1]", map);
// setValue, , p1 => ognl expression, p2 => root element, p3=> new value
Ognl.setValue("list[1]", map, "aaa");