ognl(1)

 

  1. package javaseognl;  
  2.  
  3. import ognl.OgnlContext;  
  4. import ognl.Ognl;  
  5. import ognl.OgnlException;  
  6.  
  7. /**  
  8.  * Created by IntelliJ IDEA.  
  9.  * User: Administrator  
  10.  * Date: 2011-12-29  
  11.  * Time: 15:18:46  
  12.  * To change this template use File | Settings | File Templates.  
  13.  */ 
  14.  
  15. /**  
  16.  * person bean  
  17.  */ 
  18. class Person {  
  19.     private String name;  
  20.  
  21.     public String getName() {  
  22.         return name;  
  23.     }  
  24.  
  25.     public void setName(String name) {  
  26.         this.name = name;  
  27.     }  
  28. }  
  29.  
  30. /**  
  31.  * dog bean  
  32.  */ 
  33. class Dog {  
  34.     private String name;  
  35.  
  36.     public String getName() {  
  37.         return name;  
  38.     }  
  39.  
  40.     public void setName(String name) {  
  41.         this.name = name;  
  42.     }  
  43. }  
  44. /**  
  45.  * Ognl測試類  
  46.  */ 
  47. public class OgnlTest {  
  48.     public static void main(String[] args) throws OgnlException {  
  49.  
  50.         /**  
  51.          * 創建Ognl上下文對象  
  52.          */ 
  53.         OgnlContext context = new OgnlContext();  
  54.  
  55.         Person person = new Person();  
  56.         Dog dog = new Dog();  
  57.         person.setName("zhangsan");  
  58.         dog.setName("wangcai");  
  59.         /**  
  60.          * 把創建的對象放入Ognl上下文對象中  
  61.          */ 
  62.         context.put("person", person);  
  63.         context.put("dog", dog);  
  64.         /**  
  65.          * 設置根對象  
  66.          */ 
  67.         context.setRoot(person);  
  68.         /**  
  69.          * 解析參數根對象中查找  
  70.          */ 
  71.         Object object = Ognl.parseExpression("name");  
  72.         Object o = Ognl.getValue(object, context, context.getRoot());  
  73.         /**  
  74.          * 取出根對象的屬性  
  75.          */ 
  76.         System.out.println(o);  
  77.         /**  
  78.          *獲取person對象中的屬性   
  79.          */ 
  80.         Object object1 = Ognl.parseExpression("#person.name");  
  81.         Object o1 = Ognl.getValue(object1, context, context.getRoot());  
  82.         System.out.println(o1);  
  83.  
  84.         /**  
  85.          * 獲取dog對象中的屬性  
  86.          */ 
  87.         Object object2 = Ognl.parseExpression("#dog.name");  
  88.         Object o2 = Ognl.getValue(object2, context, context.getRoot());  
  89.         System.out.println(o2);  
  90.  
  91.     }  
  92. }  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章