本文主要是介绍ognl总结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
// ***************** root对象的概念 ******************* //public void testOgnl_01() throws Exception{User user = new User();user.setUsername("张三");//相当于调用user.getUsername()方法String value = (String)Ognl.getValue("username", user);System.out.println(value);}public void testOgnl_02() throws Exception{User user = new User();Person p = new Person();p.setName("张三");user.setPerson(p);//相当于调用user.getPerson().getName()方法String value = (String)Ognl.getValue("person.name", user);System.out.println(value);}public void testOgnl_03() throws Exception{User user = new User();Person p = new Person();p.setName("张三");user.setPerson(p);//可以使用#root来引用根对象,相当于调用user.getPerson().getName()方法String value = (String)Ognl.getValue("#root.person.name", user);System.out.println(value);}
// *********************** context的概念 **********************//public void testOgnl_04() throws Exception{Person p1 = new Person();Person p2 = new Person();p1.setName("张三");p2.setName("李四");Map context = new HashMap();context.put("p1", p1);context.put("p2", p2);String value = (String)Ognl.getValue("#p1.name + ',' + #p2.name", context, new Object());System.out.println(value);}public void testOgnl_05() throws Exception{Person p1 = new Person();Person p2 = new Person();p1.setName("张三");p2.setName("李四");Map context = new HashMap();context.put("p1", p1);context.put("p2", p2);User root = new User();root.setUsername("zhangsan");String value = (String)Ognl.getValue("#p1.name + ',' + #p2.name + ',' + username", context, root);System.out.println(value);}
// ******************* OGNL赋值操作 ************************//public void testOgnl_06() throws Exception{User user = new User();//给root对象的属性赋值,相当于调用user.setUsername()Ognl.setValue("username", user, "zhangsan");System.out.println(user.getUsername());}public void testOgnl_07() throws Exception{User user = new User();Map context = new HashMap();context.put("u", user);//给context中的对象属性赋值,相当于调用user.setUsername()Ognl.setValue("#u.username",context, new Object(), "zhangsan");System.out.println(user.getUsername());}public void testOgnl_08() throws Exception{User user = new User();Map context = new HashMap();context.put("u", user);//给context中的对象属性赋值,相当于调用user.setUsername()//在表达式中使用=赋值操作符来赋值Ognl.getValue("#u.username = '张三'",context, new Object());System.out.println(user.getUsername());}public void testOgnl_09() throws Exception{User user = new User();Person p = new Person();Map context = new HashMap();context.put("u", user);context.put("p", p);//给context中的对象属性赋值,相当于调用user.setUsername()//在表达式中使用=赋值操作符来赋值Ognl.getValue("#u.username = '张三',#p.name = '李四'",context, new Object());System.out.println(user.getUsername()+","+p.getName());}
//****************** 使用OGNL调用对象的方法 **********************//public void testOgnl_10() throws Exception{User user = new User();user.setUsername("张三");String value = (String)Ognl.getValue("getUsername()", user);System.out.println(value);}public void testOgnl_11() throws Exception{User user = new User();Ognl.getValue("setUsername('张三')", user);System.out.println(user.getUsername());}
// ********************* OGNL中的this表达式 **********************//public void testOgnl_14() throws Exception{Object root = new Object();Map context = new HashMap();List values = new ArrayList();for(int i=0; i<11; i++){values.add(i);}context.put("values", values);Ognl.getValue("@System@out.println(#values.size.(#this > 10 ? \"大于10\" : '不大于10'))", context, root);}public void testOgnl_15() throws Exception{User user = new User();Ognl.getValue("setUsername('ZHANGSAN')", user);Ognl.getValue("@System@out.println(#this.username)", user);}public void testOgnl_16() throws Exception{User user = new User();Ognl.getValue("setUsername('ZHANGSAN')", user);Ognl.getValue("@System@out.println(username.(#this.toLowerCase()))", user);}
// ******************* 如何把表达式的解释结果作为另外一个表达式,OGNL中括号的使用 **********************//public void testOgnl_17() throws Exception{Object root = new Object();Map context = new HashMap();User u = new User();u.setUsername("张三");context.put("u", u);context.put("fact", "username");/*** 1、首先把#fact表达式进行解释,得到一个值:username* 2、解释括号中的表达式#u,其结果是user对象* 3、把括号中表达式的结果作为root对象,解释在第一步中得到的结果(即把第一步的结果看成另外一个表达式) */String value = (String)Ognl.getValue("#fact(#u)", context, root);System.out.println(value);}public void testOgnl_18() throws Exception{Person person = new Person();Map context = new HashMap();User u = new User();u.setUsername("张三");context.put("u", u);/*** 相当于调用person这个根对象的fact方法,并把#u的解释结果作为参数传入此方法 */String value = (String)Ognl.getValue("fact(#u)", context, person);System.out.println(value); //输出是 "张三,"}public void testOgnl_19() throws Exception{Person person = new Person();Map context = new HashMap();User u = new User();u.setUsername("张三");context.put("u", u);/*** 1、先计算表达式fact,得到结果是:username* 2、解释括号中的表达式#u,其结果是user对象* 3、把括号中表达式的结果作为root对象,解释在第一步中得到的结果(即把第一步的结果看成另外一个表达式)*/String value = (String)Ognl.getValue("(fact)(#u)", context, person);System.out.println(value); //输出"张三"}
// ********************* OGNL访问集合元素 **************************//public void testOgnl_20() throws Exception{Object root = new Object();Map context = new HashMap();//用OGNL创建List对象List listvalue = (List)Ognl.getValue("{123,'kdjfk','oooo'}", context, root);context.put("listvalue", listvalue);//用OGNL创建数组int[] intarray= (int[])Ognl.getValue("new int[]{23,45,67}", context, root);context.put("intarray", intarray);//用OGNL创建Map对象Map mapvalue = (Map)Ognl.getValue("#{'listvalue':#listvalue,'intarray':#intarray}", context, root);context.put("mapvalue", mapvalue);Ognl.getValue("@System@out.println(#listvalue[0])", context, root);Ognl.getValue("@System@out.println(#intarray[1])", context, root);Ognl.getValue("@System@out.println(#mapvalue['intarray'][2])", context, root);Ognl.getValue("@System@out.println(#mapvalue.intarray[0])", context, root);}
这篇关于ognl总结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!