`
tangyi198388
  • 浏览: 17658 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

ognl的学习例子

阅读更多
原贴见:http://blog.csdn.net/upccpu/archive/2005/10/09/498407.aspx

在解释OGNL表达式的时候,是在当前对象上下文中的。在一个链中,前一个对象做为解释下一个对象的上下文。你可以任意扩展这个链。例如:
name.toCharArray()[0].numericValue.toString()
解释步骤如下:
1)   从跟或初始对象提取name属性(初始或跟对象可以通过OGNL Context设置)
2)   调用name的toCharArray()方法
3)   取toCharArray()方法返回的第一个字符对象(Character)
4)   调用Character的getNumericValue()方法
最后调用toString()方法

一、OGNL处理不同对象的属性是不一样的。
1.   Map的属性就是key
2.   Lists和Array的属性就是下标index
3.   String的属性也是下标
4.   普通对象只能处理string属性,这个属性具有set/get或is/set方法。

属性名可以是任何类型,而不仅仅是“字符串类型的名字”(注意区别属性名和属性值的类型,这里是说这个属性的名字可以是非字符串的,例如这个属性的名字可以是一个数字),对于非字符串类型的属性名,我们访问属性值的时候要用下标的形式。例如对一个数组array:
它的长度是array的一个属性,这个属性名字叫做length,这个名字是一个字符串,因此可以用下面的方式访问:
array.length

现在要访问第0个元素,那么这个属性名是0,它是数字,应该按下下标方式访问
array[0]
OGNL内部是将array.length
做为array[“length”]形式的,同时也等价于array[“len”+”gth”]


OGNL对index方式访问属性做了扩展,它不仅仅可以用整数下标,也可以使用任意的对象做index。OGNL将匹配java对象中下列形式的方法:
public PropertyType getPropertyName(IndexType index)
public void setPropertyName(IndexType index, PropertyType value)
PropertyType 和 IndexType必须在相应的set和get方法中匹配。在webwork中的Session对象就有:
    public Object getAttribute(String name)

public void setAttribute(String name, Object value)

来设置任意类型的属性值。使用OGNL表达式来访问就是:
    session.attribute[“foo”]

反应ognl特点的例子:
public class Root {
    public static final String      SIZE_STRING = "size";

    private int[]  array = { 1, 2, 3, 4 };

    private Map  map = new HashMap(23);

    private List list = Arrays.asList(new Object[] { null, this, array });

    public Root(){

         map.put( "test", this );

         map.put( "array", array );

         map.put( "list", list );

         map.put( "size", new Integer(5000) );

         map.put( DynamicSubscript.first, new Integer(99) );

    }
      .......set,get方法
}

测试:
public class RootTest extends TestCase {

    private OgnlContext context;

    private static Root ROOT = new Root();

    public void testProperties()throws Exception{
        SimpleNode expression;

        //直接用Root中map属性的名字来访问
        expression = (SimpleNode) Ognl.parseExpression("map");
        assertTrue(Ognl.getValue(expression, context, ROOT) == ROOT.getMap());
        //访问Root中map属性的test属性
        expression = (SimpleNode) Ognl.parseExpression("map.test");
        assertTrue(Ognl.getValue(expression, context, ROOT).equals(ROOT));
        //上面用map.test来访问,现在用下标形式访问
        expression = (SimpleNode) Ognl.parseExpression("map[\"test\"]");
        assertTrue(Ognl.getValue(expression, context, ROOT).equals(ROOT));
        //跟上面的是一样的,这里演示了下标计算之后,访问到的值
        expression = (SimpleNode) Ognl.parseExpression("map[\"te\" + \"st\"]"");
        assertTrue(Ognl.getValue(expression, context, ROOT).equals(ROOT));
        /*来看看对size的访问,这里有看头,在初始化的时候是map.put( "size", new Integer(5000) );
         *很自然我们会想到用map.size或者map["size"]来访问,显然没有问题
         *这里要演示的是,怎样访问静态变量,在Root中定义了:
         *public static final String      SIZE_STRING = "size";
         *我们不可以用map[Root.SIZE_STRING]的形式访问吗?写成下面的形式:
         *expression = (SimpleNode) Ognl.parseExpression("map[Root.SIZE_STRING]");
         *OGNL就会认为有Root.SIZE_STRING这样一个对象是map的属性,而不是先去解释Root.SIZE_STRING为字符串size的
         *看看下面是怎么办的,@做为静态导航
         */
        expression = (SimpleNode) Ognl.parseExpression("map[@net.wide.ognl.bean.Root@SIZE_STRING]");
        System.out.println(Ognl.getValue(expression, context, ROOT));

        //下面通过下标访问List或者数组
        expression = (SimpleNode) Ognl.parseExpression("map.array[0]");//map.list[1]
        System.out.println(Ognl.getValue(expression, context, ROOT));

        /*对DynamicSubscript的测试
         * 先看看它的代码:
         * switch (flag)
          {
            case FIRST: return "^";
            case MID:   return "|";
            case LAST:  return "$";
            case ALL:   return "*";
            default:    return "?"; // Won't happen
          }
          很清楚了!下面来试试
          在Root中有这么一个初始化的地方:
          map.put( DynamicSubscript.first, new Integer(99) );
          我们通过OGNL表达式怎么访问呢?

          对于一个数组或List应用上面的表达式,则是取出在这个列表中对应位置的元素
          在Map中我们需要显示地使用DynamicSubscript.first等做为key才能取得到值
         */
        expression = (SimpleNode) Ognl.parseExpression("map[^]");

        System.out.println("first-^:" + Ognl.getValue(expression, context, ROOT));

        expression = (SimpleNode) Ognl.parseExpression("map.array[|]");

        System.out.println("middle-|:" + Ognl.getValue(expression, context, ROOT));

        expression = (SimpleNode) Ognl.parseExpression("map.array[$]");

        System.out.println("last-$:" + Ognl.getValue(expression, context, ROOT));

        expression = (SimpleNode) Ognl.parseExpression("map.array[*]");

        System.out.println("all-*:" + Ognl.getValue(expression, context, ROOT));

        //测试数组或列表的伪属性
        expression = (SimpleNode) Ognl.parseExpression("map.array.length");

        System.out.println("array length:" + Ognl.getValue(expression, context, ROOT));


        /* 看看下面有这么一个东东:
         * map.(array[2] + size()).doubleValue()
         * 在前面的学习中,我们了解了OGNL的导航链,解析链中的属性或方法都是基于当前解释出来的结果的
         * 因此array[2]就是map.array[2]
         * size()就是map.size()
         * 他们相加转换成Double型。
         * 看看结果是:8.0
         */
        expression = (SimpleNode) Ognl.parseExpression("map.(array[2] + size()).doubleValue()");

        System.out.println("map.(array[2] + size()).doubleValue():" + Ognl.getValue(expression, context, ROOT));

        //map.(#this),this是对自身的引用,另外注意在变量名前加#符号,这个变量在这个表达式里面是全局的

        expression = (SimpleNode) Ognl.parseExpression("map.(#this)");

        System.out.println("map.(#this):" + Ognl.getValue(expression, context, ROOT));

        //几个OGNL表达式,下面的意思是,测试map的第一个元素是否为空,如果为空则返回empty否则返回该对象

        //这个写法我们非常熟悉,无论是java还是c都有这种写法

        expression = (SimpleNode) Ognl.parseExpression("map[^].(#this == null ? 'empty' : #this)");

        System.out.println("map[^].(#this == null ? 'empty' : #this):" + Ognl.getValue(expression, context, ROOT));

    }

}

分享到:
评论
2 楼 airlink 2009-11-17  
例子疑似不完整,缺少对context的初始化。
查了其他例子才知道是这样的:
context = (OgnlContext)Ognl.createDefaultContext(null);

可在setUp中做
1 楼 xiongpf88 2008-01-30  
呵呵,有空教我

相关推荐

Global site tag (gtag.js) - Google Analytics