public class OGNLDemo3 {
//测试赋值
@Test
public void testOGNL1() throws Exception{
User user = new User();
Ognl.setValue("username", user, "张三");
System.out.println(user.getUsername());
}
@Test
public void testOGNL2() throws Exception{
User user = new User();
Map context = new HashMap();
context.put("u1", user);
Ognl.setValue("#u1.username", context, new Object(), "张三");
System.out.println(user.getUsername());
}
@Test
public void testOGNL3() throws Exception{
User user = new User();
Map context = new HashMap();
context.put("u1", user);
//getValue也可以赋值:利用"="进行赋值
String value = (String) Ognl.getValue("#u1.username='李四'", context, new Object());
System.out.println(user.getUsername());
System.out.println(value);
}
@Test
public void testOGNL4() throws Exception{
User user = new User();
User user2 = new User();
Map context = new HashMap();
context.put("u1", user);
context.put("u2", user2);
//用逗号分隔,同时执行多个表达式
String value = (String) Ognl.getValue("#u1.username='李四',#u2.username='王五'", context, new Object());
System.out.println(user.getUsername());
System.out.println(value);
}
}
④ 使用OGNL调用对象中的方法
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-31887-3.html
很棒哦