随笔-7  评论-4  文章-0  trackbacks-0
    OGNL: Object Graph Navigation Language  (see http://www.ognl.org/ for the full documentation of OGNL). 
    Struts2框架使用标准命名上下文来使用OGNL表达式,OGNL表达式处理的顶层对象是Map(通常被称为上下文Map或上下文)。OGNL认为在应用上下文中有一个根对象(或默认对象)。即不需要使用任何特殊的标记就能够获得根对象中的属性,如果要获得其他的对象,则需要使用标记#。
    Struts2框架把OGNL context作为我们的ActionContext,并且把值栈(ValueStack)作为OGNL的根对象(ValueStack是多个对象的集合,但是对OGNL来说,它是一个单一的对象)。框架把其他对象和ValueStack一起放到ActionContext中,包括展现application、session和request 上下文的Maps,这些对象同ValueStack一起,共同存在于ActionContext中。

     |
                     |--application
                     |
                     |--session
       context map---|
                     |--value stack(root)
                     |
                     |--request
                     |
                     |--parameters
                     |
                     |--attr (searches page, request, session, then application scopes)
                     |

    Action实例总是被放入ValueStack中,因为Action在vs中,并且vs是OGNL的根对象,所以在访问Action的属性时可以忽略“ # ”标记。但是,在访问ActionContext中的其他对象时必须要使用“#”,这样OGNL才能知道我们想要访问的是Actioncontext中的其他对象,而不去根对象中查找。
    
访问Action中的一个属性
<s:property value="postalCode"/>


    Actioncontext中的其他非根对象属性,可以用“#”标记来获得。
    <s:property value="#session.mySessionPropKey"/> or
    <s:property value="#session['mySessionPropKey']"/> or
    <s:property value="#request['myRequestPropKey']"/>
同样Action可以通过一个静态的方法来获取ActionContext:
Actioncontext.getContext();
ActionContext.getContext().getSession().put("mySessionPropKey", mySessionObject);

Collections (Maps, Lists, Sets)

在框架中会经常遇到处理集合 Collections (Maps, Lists, and Sets), 下面列出几个使用select 标签处理集合的列子.  OGNL documentation 也包括许多列子.

Syntax for list: {e1,e2,e3}. 这个语法创建了一个包含字符串“name1”,“name2”和“name3”的List,并且选择“name2”作为默认值。

<s:select label="label" name="name" list="{'name1','name2','name3'}" value="%{'name2'}" />

Syntax for map: #{key1:value1,key2:value2}. 这个语法创建了一个Map键值对集合,“foo”对应“foovalue”,“bar”对应“barvalue”:

<s:select label="label" name="name" list="#{'foo':'foovalue', 'bar':'barvalue'}" />

判断一个元素是否存在于一个集合中,使用in 或 not in 操作。
<s:if test="'foo' in {'foo','bar'}">
   muhahaha
</s:if>
<s:else>
   boo
</s:else>

<s:if test="'foo' not in {'foo','bar'}">
   muhahaha
</s:if>
<s:else>
   boo
</s:else>

选择集合的一个子集(也叫投影),可以使用通配符:

  • ? - 所有元素匹配选择逻辑
  • ^ - 只有第一个元素匹配选择逻辑
  • $ - 只有最后一个元素匹配选择逻辑
  • 例如,获得人的亲属集合中为男性的一个子集

    person.relatives.{? #this.gender == 'male'}
    

    Lambda Expressions

    OGNL supports basic lamba expression syntax enabling you to write simple functions.

    (Dedicated to all you math majors who didn't think you would ever see this one again.)

    Fibonacci: if n==0 return 0; elseif n==1 return 1; else return fib(n-2)+fib(n-1);
    fib(0) = 0
    fib(1) = 1
    fib(11) = 89

    How the expression works

    The lambda expression is everything inside the square brackets. The #this variable holds the argument to the expression, which in the following example is the number 11 (the code after the square-bracketed lamba expression, #fib(11)).

    <s:property value="#fib =:[#this==0 ? 0 : #this==1 ? 1 : #fib(#this-2)+#fib(#this-1)], #fib(11)" />
    
    posted on 2009-11-07 20:56 bobby 阅读(408) 评论(0)  编辑  收藏 所属分类: Struts2

    只有注册用户登录后才能发表评论。


    网站导航: