ognl分析及用法

OGNL详细分析
一.表达式语言
属性名称
数组元素[]
方法调用()
创建集合{}
创建映射#{}
就算数学表达式结果
例题解析
1. 最简单表达式求解
     输入
     Double doubleExpression= (Double) Ognl.getValue("3.4*23+12", null);
     System.out.println(doubleExpression);
     输出
      90.2
2.创建集合
       输入
       List list= (List) Ognl.getValue("{12,34,45,56}", null);
       System.out.println(list);
       输出
         [12, 34, 45, 56]
3.创建映射
     输入
     Map map= (Map) Ognl.getValue("#{'12':12,'34':34,'56':56}", null);
     System.out.println(map);
     输出
        {12=12, 34=34, 56=56}
4.属性名称
       输入
              Cat.java
public class Cat {
        String name;
        int age;

       public String getName() {
      return name;
        }
       public void setName(String name) {
      this.name = name;
        }
       public int getAge() {
      return age;
        }
       public void setAge(int age) {
     this.age = age;
       }
}
            test.class  //测试代码
Cat cat = new Cat();
            cat.setAge(2);
            cat.setName("Tom");    
            String create= (String) Ognl.getValue("name", cat);
            System.out.println("name:" + create);
            Integer age = (Integer)Ognl.getValue("age", cat);
            System.out.println("age:"+age);
       输出
            name:Tom
age:2
5.数组元素访问
          输入
           String array[] = new String[]{"12","34","56","78"};
           String element =(String)Ognl.getValue("{'12','34','56','78'}[2]", null);
    System.out.println(element);
          输出
           56
6.context访问方式
          输入
             还是使用上面的Cat类,
             请看下面的Test代码
               Cat cat1 = new Cat();
               cat1.setAge(2);
               cat1.setName("Tom");    
    
              Cat cat2 = new Cat();
              cat2.setAge(1);
              cat2.setName("john");
              Map map = new HashMap();
              map.put("john", cat2);
              String element =(String)Ognl.getValue("#john.name", map,cat1);
              System.out.println(element);         
输出
  john
二:#的用法
1.访问context中非根元素
2.创建映射map


三.主要操作类分析
OGNL类
public static Object getValue(String expression, Object root) throws OGNLException
expression 指OGNL表达式;
root指操作对象,一般指包含属性和属性设置方法的操作对象,或者是key-value对的map方法

public static Object getValue(String expression, Object root, Class resultType) throws OGNLException
expression 指OGNL表达式;
root指操作对象
resultType指转换成的结果类型

public static Object getValue(Object tree, Object root, Class resultType) throws OGNLException
tree 指OGNL表达式,=parseExpression(expression);
root指操作对象
resultType指转换成的结果类型

public static Object getValue(Object tree, Map context, Object root, Class resultType)
tree 指OGNL表达式,=parseExpression(expression);
context 指OGNL上下文,通常存储要操作的对象,createDefaultContext(context)
root指操作对象
resultType指转换成的结果类型

public static Map addDefaultContext(Object root, map context)

public static Map addDefaultContext(Object root, ClassResolver classResolver, TypeConverter converter,
MemberAccess memberAccess, map context)
classResolver:ClassResover  Class classForName(String className, Map map)
converter:TypeConverter Object convertValue(Map context, Object target, Member member,
String propertyName, Object value, Class toType)
memberAccess:MemberAccess  public Object setup(Map context, Object target, Member member, String propertyName)
                           public void restore(Map context, Object target, Member member, String propertyName, Object state);
                           public boolean isAccessible(Map context, Object target, Member member, String propertyName);


public static void setValue( Object tree, Object root, Object value ) throws OgnlException
public static void setValue( Object tree, Map context, Object root, Object value ) throws OgnlException
public static void setValue( String expression, Object root, Object value ) throws OgnlException
public static void setValue( String expression, Map context, Object root, Object value ) throws OgnlException

OgnlContext:Map
public void setValues(Map value)
public void setClassResolver(ClassResolver vlaue)
public void setTypeConverter(TypeConverter value)
public void setMemberAccess(MemberAccess value)
public void setRoot(Object root)

Node
public Object getValue(OgnlContext context, Object source)
public void setValue(OgnlContext context ,Object target, Object value)

你可能感兴趣的:(Ognl)