②Ognl.getValue(String expression, Map context, Object root)
context:在OGNL的表达式中,有可能需要访问到多个毫不相干的对象,这时候,我们需要给OGNL传递一个Map类型的对象,把表达式中需要用到的对象放到Map中即可!这个Map对象,称为context。
要在表达式中访问到context中的对象,需要使用“#对象名称”的语法规则。ognl表达式
public class OGNLDemo2 {
@Test
public void testOGNL1() throws Exception{
User user = new User();
user.setUsername("张三");
User user2 = new User();
user2.setUsername("李四");
Map<String, User> context = new HashMap();
context.put("u1", user);
context.put("u2", user2);
//#对象名称:去访问context中的对象
//如果表达式中没有用到root对象,那么可以用任意一个对象代表root对象!
String value = (String) Ognl.getValue("#u1.username+','+#u2.username", context, new Object());
System.out.println(value);
}
@Test
public void testOGNL2() throws Exception{
User user = new User();
user.setUsername("张三");
Group g = new Group();
Organization o = new Organization();
o.setOrgId("Y11");
g.setOrg(o);
user.setGroup(g);
User user2 = new User();
user2.setUsername("李四");
User user3 = new User();
user3.setUsername("王五");
Map<String, User> context = new HashMap<String, User>();
context.put("u1", user);
context.put("u2", user2);
//#对象名称:去访问context中的对象
String value = (String) Ognl.getValue("#u1.username+','+#u2.username", context, user3);
System.out.println(value);
}
}
③ 赋值:setValue方法和 = 运算符
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-31887-2.html
你会越来约差
还有斗地主