27 4月 2011

OGNL note

Basic Syntax:
用“.“取得property,例如:user.name => user.getName();
最後加上“()“則代表呼叫method,例如:user.name.toString()
陣列的使用則是以"[]"來操作, users[0]

利用'@'來指定特定的Class、Method及Property,
例:呼叫static method @my.package.Utils@staticMethod(args)取得static property @my.package.Utils@myproperty

在OGNL的概念裡,所有變數都是global variable,但有一個特定的變數做為root
要取得變數以'#'來標示,如:#currentUser,
若是僅見到currentUser,代表是呼叫root.getCurrentUser()以取得root的currentUser

Constant:
String : 用''或““標示字串,{'name',"password"}
Character : 用''標示字元Number : 一般int, long, float, double不用特別標記,但可以用'b'或'B'標示BigDecimal
Boolean : 以true, false標示Null : 以null標示
Variable:利用#取值,可以用#this代表現有的物件也可以為variable設值,如#var1='adam';

Collection:
建立Object Array : {"name",'password'}建立指定型別的Array : new int[] {100,200,300}, new int[3] => new int[] {0,0,0}
建立Map : #{ "name":"adam", "password":"psadam" }建立指定型別的Map : #@java.util.HashMap@{ "name":"adam", "password":"psadam" }

Selecting:
可利用條件篩選Collection裡的物件,如 : users.{? #this.age > 18},代表只有age大於18的人才放入結果
還可以利用類似expression的'^'或'$'來取得第一個或最後一個符合條件的物件:users.{^ #this.age > 18}, users.{$ #this.age > 18}

Projection:
OGNL提供了Projection,也就是可以針對collection裡的物件呼叫相同的method或取得特定的property值,並將其置入另一個Collection中
如:users.{name},這代表將取出所有user的name。
由於OGNL也提供了#this,所以也可以見到這樣的用法 : users.{#this.age > 18 ? #this : null},代表只有age大於18的人才放入projection,不然就放入null

特殊property:

  • Collection:(1)size(2)isEmpty
  • List:(1)iterator
  • Map:(1)keys(2)values
  • Set:(1)iterator
  • Iterator:(1)next(2)hasNext
  • Enumeration:(1)next(2)hasNext(3)nextElement(4)hasMoreElements

容易混淆的Operator

  • Index : array[index]
  • Projection : collection.{exp}
  • Selection : collection.{? exp}

 

 

 

沒有留言: