OGNL使用小結(一)
最近一直使用struts2,表現(xiàn)層采用JSP,用的struts2標簽,支持
OGNL。
OGNL中的# $ %使用場景:
1、“#”主要有三種用途:
- 訪問OGNL上下文和Action上下文,#相當于ActionContext.getContext();下表有幾個ActionContext中有用的屬性:
名稱 |
作用 |
例子 |
parameters |
包含當前HTTP請求參數(shù)的Map |
#parameters.id[0]作用相當于request.getParameter("id") |
request |
包含當前HttpServletRequest的屬性(attribute)的Map |
#request.userName相當于request.getAttribute("userName") |
session |
包含當前HttpSession的屬性(attribute)的Map |
#session.userName相當于session.getAttribute("userName") |
application |
包含當前應用的ServletContext的屬性(attribute)的Map |
#application.userName相當于application.getAttribute("userName") |
attr |
用于按request > session > application順序訪問其屬性(attribute) |
#attr.userName相當于按順序在以上三個范圍(scope)內(nèi)讀取userName屬性,直到找到為止 |
- 用于過濾和投影(projecting)集合,如books.{?#this.price<100};
- 構造Map,如#{'foo1':'bar1', 'foo2':'bar2'}。
2、“%”符號的用途
在標志的屬性為字符串類型時,計算
OGNL表達式的值。例如在
Ognl.jsp中加入以下代碼:
[size=13px]
<hr />
<h3>%的用途</h3>
<p><s:url value="#foobar['foo1']"
/></p>
<p><s:url value="%{#foobar['foo1']}"
/></p>
清單6 演示%用途的代碼片段
3、“$”有兩個主要的用途
- 用于在國際化資源文件中,引用OGNL表達式,例子請參考《在Struts 2.0中國際化(i18n)您的應用程序》
- 在Struts 2配置文件中,引用OGNL表達式,如 <action name="AddPhoto" class="addPhoto">
<interceptor-ref name="fileUploadStack" />
<result type="redirect">ListPhotos.action?albumId=${albumId}</result>
</action>
OGNL使用小結(二)
1、OGNL除了支持所有的Java操作符外,還支持以下幾種:
1、逗號,
與C語言中的逗號操作符類似。
2、花括號{}
用于創(chuàng)建列表,元素之間用逗號分隔。
3、in和not in
用于判斷一個值是否在集合中。
2、訪問靜態(tài)方法和靜態(tài)字段
@class@method(args) //調(diào)用靜態(tài)方法
@class@field //調(diào)用靜態(tài)字段
其中class必須給出完整的類名(包括包名),如果省略class,那么默認使用的類是java.util.Math,如:
@@min(5,3)
@@max(5,3)
@@PI
3、索引訪問
OGNL支持多種索引方式的訪問。
1、數(shù)組和列表索引
在OGNL中,數(shù)組和列表可以大致看成是一樣的。
如:array[0]、list[0]。表達式:{’zhangsan’,'lisi’,'wangwu’}[1]等。
2、JavaBean的索引屬性
要使用索引屬性,需要提供兩對setter和getter方法,一對用于數(shù)組,一對用于數(shù)組中的元素。
如:有一個索引屬性interest,它的getter和setter如下
public String[] interest;
public String[] getInterest(){ return interest;}
public void setInterest(String[] interest){ this.interest=interest;}
public String getInterest(int i){ return interest[i]}
public void setInterest(int i, String newInterest){ interest[i]=newInterest;}
對于表達式interest[2],OGNL可以正確解釋這個表達式,調(diào)用getInterest(2)方法。如果是設置的情況下,會調(diào)用setInterest(2,value)方法。
3、OGNL對象的索引屬性
JavaBean的索引屬性只能使用整型作為索引,OGNL擴展了索引屬性的概念,可以使用任意的對象來作為索引。
4、對集合進行操作
1、創(chuàng)建集合:
創(chuàng)建列表
使用花括號將元素包含起來,元素之間使用逗號分隔。如{’zhangsan’,'lisi’,'wangwu’}
創(chuàng)建數(shù)組
OGNL中創(chuàng)建數(shù)組與Java語言中創(chuàng)建數(shù)組類似。
創(chuàng)建Map
Map使用特殊的語法來創(chuàng)建 #{”key”:value, ……}
如果想指定創(chuàng)建的Map類型,可以在左花括號前指定Map實現(xiàn)類的類名。如:
#@java.util.LinkedHashMap@{”key”:”value”,….}
Map通過key來訪問,如map["key"]或map.key。
2、投影
OGNL提供了一種簡單的方式在一個集合中對每一個元素聞調(diào)用相同的方法,或者抽取相同的屬性,并將結果保存為一個新的集合,稱之為投影。
假如employees是一個包含了employee對象的列表,那么
#employees.{name}將返回所有雇員的名字的列表。
在投影期間,使用#this變量來引用迭代中的當前元素。
如:objects.{#this instanceof String? #this: #this.toString()}
3、選擇
OGNL提供了一種簡單的方式來使用表達式從集合中選擇某些元素,并將結果保存到新的集合中,稱為選擇。
如#employees.{?#this.salary>3000}
將返回薪水大于3000的所有雇員的列表。
#employees.{^#this.salary>3000}
將返回第一個薪水大于3000的雇員的列表。
#employees.{$#this.salary>3000}
將返回最后一個薪水大于3000的雇員的列表。
5、lambda表達式
lambda表達式的語法是: :[...]。OGNL中的lambda表達式只能使用一個參數(shù),這個參數(shù)通過#this引用。
如:
#fact= :[ #this<=1 ? 1 : #this* #fact ( #this-1) ], #fact(30)
#fib= :[#this==0 ? 0 : #this==1 ? 1 : #fib(#this-2)+#fib(#this-1)], #fib(11)