Struts2学习6—OGNL (1)

http://struts.apache.org/release/2.2.x/docs/ognl.html

OGNL is the Object Graph Navigation Language.

在ActionContext中,OGNL的根对象为值栈,运行的时候,action的实例将放到值栈里。

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

如果访问值栈内的对象,可直接引用。如果是非值栈内的对象,应该加#。

<s:property value="postalCode"/>
<s:property value="#session.mySessionPropKey"/> or
<s:property value="#session['mySessionPropKey']"/> or
<s:property value="#request['myRequestPropKey']"/>

Actioncontext也可从静态方法中得到

ActionContext.getContext().getSession().put("mySessionPropKey", mySessionObject);

如果表达式不支持动态内容,可以用以下方式

<c:set var="foo" value="bar" scope="request"/>
<s:textfield name="username" label="%{#request.foo}" />

集合的处理

用list的代码:{e1,e2,e3}

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

用Map的代码:#{key1:value1,key2:value2}

<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>

子集运算可用

  1. ? 匹配所有满足条件的
  2. ^  第一个
  3. $  最后一个
person.relatives.{? #this.gender == 'male'}

支持Lamda表达式,Lamda表达式用【】

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

<s:property value="#fib =:[#this==0 ? 0 : #this==1 ? 1 : #fib(#this-2)+#fib(#this-1)], #fib(11)" />

OGNL在tag中的书写方式

1)tag的规则:大意思是,字符串解析成"%{ ... }" ,非字符串直接为表达式值,如果非字符串用了"%{ ... }" ,符号会忽略。

  1. All String attribute types are parsed for the "%{ ... }" notation.
  2. All non-String attribute types are not parsed, but evaluated directly as an expression
  3. The exception to rule #2 is that if the non-String attribute uses the escape notion "%{}", the notation is ignored as redundant, and the content evaluated.

http://struts.apache.org/release/2.3.x/docs/tag-syntax.html

2)Value的值是对象

<s:textfield key="state.label" name="state" value="ca"/>错

<s:textfield key="state.label" name="state" value="%{'ca'}" />
<p>Username: ${user.username}</p>
EL表达式,非OGNL
<s:textfield name="username"/>
Value Stack中的值
<s:url id="es" action="Hello">
  <s:param name="request_locale">
    es
  </s:param>
</s:url>
<s:a href="%{es}">Espanol</s:a>
 
<s:property  value="#session.user.username" />
Seesion中的值
<s:select  label="FooBar" name="foo"   list="#{'username':'trillian',    'username':'zaphod'}" />
Map对象,非VauleStack中值
<s:iterator value="cart.items">
   ...
   <s:textfield label="'Cart item No.' + #rowstatus.index + ' note'"
                 name="'cart.items[' + #rowstatus.index + '].note'"
                 value="note" />
</s:iterator>
<s:iterator value="cart.items">
   ...
   <s:textfield label="Cart item No. %{#rowstatus.index} note"
                 name="cart.items[%{#rowstatus.index}].note"
                 value="%{note}" />
</s:iterator>
左右两边是相等的。右边更好
原文地址:https://www.cnblogs.com/meetcomet/p/3408316.html