OGNL is the Object Graph Navigation Language (see http://www.ognl.org/
for the full documentation of OGNL). Here, we will cover a few examples
of OGNL features that co-exist with the framework. To review basic
concepts, refer to OGNL Basics.
OGNL是對(duì)象圖形定位語言.這里將設(shè)計(jì)一個(gè)OGNL和框架并存的例子。
The framework uses a standard naming context to evaluate OGNL
expressions. The top level object dealing with OGNL is a Map (usually
referred as a context map or context). OGNL has a notion of there being
a root (or default) object within the context. In expression, the
properties of the root object can be referenced without any special
"marker" notion. References to other objects are marked with a pound
sign (#).
框架用一個(gè)標(biāo)準(zhǔn)命名上下文來計(jì)算OGNL表達(dá)式。處理OGNL的高層對(duì)象是Map。OGNL在上下文內(nèi)有個(gè)根的概念。在表達(dá)式里,不需要任何“標(biāo)志”關(guān)聯(lián)到根對(duì)象的屬性。關(guān)聯(lián)到其他對(duì)象得用#標(biāo)識(shí)。
The framework sets the OGNL context to be our ActionContext, and the
value stack to be the OGNL root object. (The value stack is a set of
several objects, but to OGNL it appears to be a single object.) Along
with the value stack, the framework places other objects in the
ActionContext, including Maps representing the application, session,
and request contexts. These objects coexist in the ActionContext,
alongside the value stack (our OGNL root).
框架設(shè)置OGNLcontext為我們的 ActionContext, value stack 是OGNL根對(duì)象。(value stack是一系列的值,但對(duì)OGNL,它表現(xiàn)為一個(gè)對(duì)象)連同value stack,框架放置其他對(duì)象到ActionContext,包括展示application,session和request上下文。這些對(duì)象與value stack一起共存在ActionContext。
There are other objects in the context map. The diagram is for example only.
The Action instance is always pushed onto the value stack. Because
the Action is on the stack, and the stack is the OGNL root, references
to Action properties can omit the # marker. But, to access other objects in the ActionContext, we must use the # notation so OGNL knows not to look in the root object, but for some other object in the ActionContext.
Action 實(shí)例被推入value stack。因?yàn)锳ction在stack里,并且statck是OGNL的根,和Action關(guān)聯(lián)的屬性忽略#標(biāo)記。但訪問ActionContext的其他對(duì)象,我們必須用#標(biāo)識(shí),OGNL知道不應(yīng)該區(qū)根對(duì)象查找,而是到ActionCopntext的其他對(duì)象里查找。
<s:property value="postalCode"/>
Other (non-root) objects in the ActionContext can be rendered use the # notation.
<s:property value="#session.mySessionPropKey"/> or
<s:property value="#session["mySessionPropKey"]"/> or
<s:property value="#request["mySessionPropKey"]/>
The ActionContext is also exposed to Action classes via a static method.
ActionContext也通過靜態(tài)方法被暴露給Action類
ActionContext.getContext().getSession().put("mySessionPropKey", mySessionObject);
Collections (Maps, Lists, Sets)
Dealing with Collections (Maps, Lists, and Sets) in the framework comes often, so here are a few examples using the select tag.
聲明集合在框架中經(jīng)常用到,所以這里是用select標(biāo)簽的例子。
Syntax for list: {e1,e2,e3}. This idiom creates a List containing
the String "name1", "name2" and "name3". It also selects "name2" as the
default value.
list語法:{e1,e2,e3}.這個(gè)習(xí)慣創(chuàng)建一個(gè)包括“name1”,“name2”和“name3”。它也選擇“name2”作為默認(rèn)值。
<s:select label="label" name="name" list="{'name1','name2','name3'}" value="%{'name2'}" />
Syntax for map: #{key1:value1,key2:value2}. This idiom creates a map
that maps the string "foo" to the string "foovalue" and "bar" to the
string "barvalue":
map語法:#{key1:value1,key2:value2}.這個(gè)創(chuàng)建map,把字符傳“foo”到字符串"foovalue",“bar”到字符串"barvalue".
<s:select label="label" name="name" list="#{'foo':'foovalue', 'bar':'barvalue'}" />
To determine if an element exists in a Collection, use the operations in and not in.
用in 和 not in 檢驗(yàn)元素是否在一個(gè)集合里面
<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>
To select a subset of a collection (called projection), use a wildcard within the collection.
- ? - All elements matching the selection logic
- ^ - Only the first element matching the selection logic
- $ - Only the last element matching the selection logic
To obtain a subset of just male relatives from the object person:
person.relatives.{? #this.gender == 'male'}
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 brackets. The #this
variable holds the argument to the expression, which is initially
starts at 11.
|
<s:property value="#fib =:[#this==0 ? 0 : #this==1 ? 1 : #fib(#this-2)+#fib(#this-1)], #fib(11)" />
JSP 2.1
Under JSP 2.1 the # character is now used by the JSP EL.
This may cause problems with some applications that use the OGNL # operator.
One quick-fix is to disable the JSP EL in a JSP 2.1 container (like GlassFish) by adding a jsp-config element to the web.xml
和JSP2.1的兼容問題處理
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<el-ignored>true</el-ignored>
</jsp-property-group>
</jsp-config>