现在的位置: 首页 > 综合 > 正文

ognl总结

2018年06月29日 ⁄ 综合 ⁄ 共 11680字 ⁄ 字号 评论关闭

ognl 方法总结

 

Java代码 复制代码
  1. // ***************** root对象的概念 ******************* //   
  2. public void testOgnl_01() throws Exception{   
  3.     User user = new User();   
  4.     user.setUsername("张三");   
  5.        
  6.     //相当于调用user.getUsername()方法   
  7.     String value = (String)Ognl.getValue("username", user);   
  8.     System.out.println(value);   
  9. }   
  10.   
  11. public void testOgnl_02() throws Exception{   
  12.     User user = new User();   
  13.     Person p = new Person();   
  14.     p.setName("张三");   
  15.     user.setPerson(p);   
  16.        
  17.     //相当于调用user.getPerson().getName()方法   
  18.     String value = (String)Ognl.getValue("person.name", user);   
  19.     System.out.println(value);   
  20. }   
  21.   
  22. public void testOgnl_03() throws Exception{   
  23.     User user = new User();   
  24.     Person p = new Person();   
  25.     p.setName("张三");   
  26.     user.setPerson(p);   
  27.        
  28.     //可以使用#root来引用根对象,相当于调用user.getPerson().getName()方法   
  29.     String value = (String)Ognl.getValue("#root.person.name", user);   
  30.     System.out.println(value);   
  31. }   

 

 

 

Java代码 复制代码
  1. // *********************** context的概念 **********************//   
  2.     public void testOgnl_04() throws Exception{   
  3.         Person p1 = new Person();   
  4.         Person p2 = new Person();   
  5.         p1.setName("张三");   
  6.         p2.setName("李四");   
  7.            
  8.         Map context = new HashMap();   
  9.         context.put("p1", p1);   
  10.         context.put("p2", p2);   
  11.            
  12.         String value = (String)Ognl.getValue("#p1.name + ',' + #p2.name", context, new Object());   
  13.         System.out.println(value);   
  14.     }   
  15.        
  16.     public void testOgnl_05() throws Exception{   
  17.         Person p1 = new Person();   
  18.         Person p2 = new Person();   
  19.         p1.setName("张三");   
  20.         p2.setName("李四");   
  21.            
  22.         Map context = new HashMap();   
  23.         context.put("p1", p1);   
  24.         context.put("p2", p2);   
  25.            
  26.         User root = new User();   
  27.         root.setUsername("zhangsan");   
  28.            
  29.         String value = (String)Ognl.getValue("#p1.name + ',' + #p2.name + ',' + username", context, root);   
  30.         System.out.println(value);   
  31.     }  

 

 

 

Java代码 复制代码
  1. // ******************* OGNL赋值操作 ************************//   
  2. public void testOgnl_06() throws Exception{   
  3.     User user = new User();   
  4.        
  5.     //给root对象的属性赋值,相当于调用user.setUsername()   
  6.     Ognl.setValue("username", user, "zhangsan");   
  7.        
  8.     System.out.println(user.getUsername());   
  9. }   
  10.   
  11. public void testOgnl_07() throws Exception{   
  12.     User user = new User();   
  13.        
  14.     Map context = new HashMap();   
  15.     context.put("u", user);   
  16.        
  17.     //给context中的对象属性赋值,相当于调用user.setUsername()   
  18.     Ognl.setValue("#u.username",context, new Object(), "zhangsan");   
  19.        
  20.     System.out.println(user.getUsername());   
  21. }   
  22.   
  23. public void testOgnl_08() throws Exception{   
  24.     User user = new User();   
  25.        
  26.     Map context = new HashMap();   
  27.     context.put("u", user);   
  28.        
  29.     //给context中的对象属性赋值,相当于调用user.setUsername()   
  30.     //在表达式中使用=赋值操作符来赋值   
  31.     Ognl.getValue("#u.username = '张三'",context, new Object());   
  32.        
  33.     System.out.println(user.getUsername());   
  34. }   
  35.   
  36. public void testOgnl_09() throws Exception{   
  37.     User user = new User();   
  38.     Person p = new Person();   
  39.        
  40.     Map context = new HashMap();   
  41.     context.put("u", user);   
  42.        
  43.     context.put("p", p);   
  44.        
  45.     //给context中的对象属性赋值,相当于调用user.setUsername()   
  46.     //在表达式中使用=赋值操作符来赋值   
  47.     Ognl.getValue("#u.username = '张三',#p.name = '李四'",context, new Object());   
  48.        
  49.     System.out.println(user.getUsername()+","+p.getName());   
  50. }   

 

 

 

Java代码 复制代码
  1.   
  2. //****************** 使用OGNL调用对象的方法 **********************//   
  3. public void testOgnl_10() throws Exception{   
  4.     User user = new User();   
  5.     user.setUsername("张三");   
  6.        
  7.     String value = (String)Ognl.getValue("getUsername()", user);   
  8.     System.out.println(value);   
  9. }   
  10.   
  11. public void testOgnl_11() throws Exception{   
  12.     User user = new User();   
  13.        
  14.     Ognl.getValue("setUsername('张三')", user);   
  15.     System.out.println(user.getUsername());   
  16. }   

 

 

 

Java代码 复制代码
  1. // ********************* OGNL中的this表达式 **********************//   
  2. public void testOgnl_14() throws Exception{   
  3.     Object root = new Object();   
  4.     Map context = new HashMap();   
  5.        
  6.     List values = new ArrayList();   
  7.     for(int i=0; i<11; i++){   
  8.         values.add(i);   
  9.     }   
  10.     context.put("values", values);   
  11.        
  12.     Ognl.getValue("@System@out.println(#values.size.(#this > 10 ? /"大于10/" : '不大于10'))", context, root);   
  13.        
  14. }   
  15.   
  16. public void testOgnl_15() throws Exception{   
  17.     User user = new User();   
  18.        
  19.     Ognl.getValue("setUsername('ZHANGSAN')", user);   
  20.     Ognl.getValue("@System@out.println(#this.username)", user);   
  21. }   
  22.   
  23. public void testOgnl_16() throws Exception{   
  24.     User user = new User();   
  25.        
  26.     Ognl.getValue("setUsername('ZHANGSAN')", user);   
  27.     Ognl.getValue("@System@out.println(username.(#this.toLowerCase()))", user);   
  28. }  

 

 

 

Java代码 复制代码
  1. // ******************* 如何把表达式的解释结果作为另外一个表达式,OGNL中括号的使用 **********************//   
  2. public void testOgnl_17() throws Exception{   
  3.     Object root = new Object();   
  4.     Map context = new HashMap();   
  5.     User u = new User();   
  6.     u.setUsername("张三");   
  7.     context.put("u", u);   
  8.     context.put("fact""username");   
  9.        
  10.     /**  
  11.      * 1、首先把#fact表达式进行解释,得到一个值:username  
  12.      * 2、解释括号中的表达式#u,其结果是user对象  
  13.      * 3、把括号中表达式的结果作为root对象,解释在第一步中得到的结果(即把第一步的结果看成另外一个表达式)   
  14.      */  
  15.     String value = (String)Ognl.getValue("#fact(#u)", context, root);   
  16.     System.out.println(value);   
  17. }   
  18.   
  19. public void testOgnl_18() throws Exception{   
  20.     Person person = new Person();   
  21.     Map context = new HashMap();   
  22.     User u = new User();   
  23.     u.setUsername("张三");   
  24.     context.put("u", u);   
  25.        
  26.     /**  
  27.      * 相当于调用person这个根对象的fact方法,并把#u的解释结果作为参数传入此方法   
  28.      */  
  29.     String value = (String)Ognl.getValue("fact(#u)", context, person);   
  30.     System.out.println(value); //输出是 "张三,"   
  31. }   
  32.   
  33. public void testOgnl_19() throws Exception{   
  34.     Person person = new Person();   
  35.     Map context = new HashMap();   
  36.     User u = new User();   
  37.     u.setUsername("张三");   
  38.     context.put("u", u);   
  39.        
  40.     /**  
  41.      * 1、先计算表达式fact,得到结果是:username  
  42.      * 2、解释括号中的表达式#u,其结果是user对象  
  43.      * 3、把括号中表达式的结果作为root对象,解释在第一步中得到的结果(即把第一步的结果看成另外一个表达式)  
  44.      */  
  45.     String value = (String)Ognl.getValue("(fact)(#u)", context, person);   
  46.     System.out.println(value); //输出"张三"   
  47. }   

 

 

 

 

 

Java代码 复制代码
  1. // ********************* OGNL访问集合元素 **************************//   
  2. public void testOgnl_20() throws Exception{   
  3.     Object root = new Object();   
  4.     Map context = new HashMap();   
  5.        
  6.     //用OGNL创建List对象   
  7.     List listvalue = (List)Ognl.getValue("{123,'kdjfk','oooo'}", context, root);   
  8.     context.put("listvalue", listvalue);   
  9.        
  10.     //用OGNL创建数组   
  11.     int[] intarray= (int[])Ognl.getValue("new int[]{23,45,67}", context, root);   
  12.     context.put("intarray", intarray);   
  13.        
  14.     //用OGNL创建Map对象   
  15.     Map mapvalue = (Map)Ognl.getValue("#{'listvalue':#listvalue,'intarray':#intarray}", context, root);   
  16.     context.put("mapvalue", mapvalue);   
  17.        
  18.     Ognl.getValue("@System@out.println(#listvalue[0])", context, root);   
  19.     Ognl.getValue("@System@out.println(#intarray[1])", context, root);   
  20.     Ognl.getValue("@System@out.println(#mapvalue['intarray'][2])", context, root);   
  21.     Ognl.getValue("@System@out.println(#mapvalue.intarray[0])", context, root);   
  22. }  

抱歉!评论已关闭.