创建对象~
测试(list):
@Test
//OGNL创建对象list|map
public void fun8() throws OgnlException {
//准备OGNLContext
//准备Root
User rootUser1 = new User("tom",18);
//准备Context
Map<String,User> context = new HashMap<String,User>();
context.put("user1", new User("isleiyi",18));
context.put("user2", new User("Rose",19));
OgnlContext oc = new OgnlContext();
oc.setRoot(rootUser1);
oc.setValues(context);
//书写OGNl
Integer size1 = (Integer) Ognl.getValue("{'tom','jerry','jack','rose'}.size()", oc, oc.getRoot());
String name1 = (String) Ognl.getValue("{'tom','jerry','jack','rose'}[0]", oc, oc.getRoot());
String name2 = (String) Ognl.getValue("{'tom','jerry','jack','rose'}.get(1)", oc, oc.getRoot());
System.out.println(size1);
System.out.println(name1);
System.out.println(name2);
}
输出:
4
tom
jerry
测试(map):
Integer size2 = (Integer) Ognl.getValue("#{'name':'tom','age':18}.size()", oc, oc.getRoot());
String name3 = (String) Ognl.getValue("#{'name':'tom','age':18}['name']", oc, oc.getRoot());
Integer name4 = (Integer) Ognl.getValue("#{'name':'tom','age':18}.get('age')", oc, oc.getRoot());
System.out.println(size2);
System.out.println(name3);
System.out.println(name4);
输出:
2
tom
18