OGNL表達(dá)式基本語(yǔ)法與用法詳解
一.OGNL中的#、%和$符號(hào)
#、%和$符號(hào)在OGNL表達(dá)式中經(jīng)常出現(xiàn),而這三種符號(hào)也是開(kāi)發(fā)者不容易掌握和理解的部分。在這里我們簡(jiǎn)單介紹它們的相應(yīng)用途。
1.#符號(hào)的三種用法
1)訪問(wèn)非根對(duì)象屬性,例如示例中的#session.msg表達(dá)式,由于Struts 2中值棧被視為根對(duì)象,所以訪問(wèn)其他非根對(duì)象時(shí),需要加#前綴。實(shí)際上,#相當(dāng)于ActionContext. getContext();#session.msg表達(dá)式相當(dāng)于ActionContext.getContext().getSession(). getAttribute("msg") 。
2)用于過(guò)濾和投影(projecting)集合,如示例中的persons.{?#this.age>20}。
3) 用來(lái)構(gòu)造Map,例如示例中的#{'foo1':'bar1', 'foo2':'bar2'}。
2.%符號(hào)
%符號(hào)的用途是在標(biāo)志的屬性為字符串類(lèi)型時(shí),計(jì)算OGNL表達(dá)式的值。如下面的代碼所示:
<h3>構(gòu)造Map</h3>
<s:set name="foobar" value="#{'foo1':'bar1', 'foo2':'bar2'}" />
<p>The value of key "foo1" is <s:property value="#foobar['foo1']" /></p>
<p>不使用%:<s:url value="#foobar['foo1']" /></p>
<p>使用%:<s:url value="%{#foobar['foo1']}" /></p>
運(yùn)行界面如下所示。
he value of key "foo1" is bar1 不使用%:#foobar['foo1'] 使用%:bar1
3.$符號(hào)
$符號(hào)主要有兩個(gè)方面的用途。
1) 在國(guó)際化資源文件中,引用OGNL表達(dá)式,例如國(guó)際化資源文件中的代碼:reg.agerange=國(guó)際化資源信息:年齡必須在
${min}同${max}之間。
2) 在Struts 2框架的配置文件中引用OGNL表達(dá)式,例如下面的代碼片斷所示:
<validators>
<field name="intb">
<field-validator type="int">
<param name="min">10</param>
<param name="max">100</param>
<message>BAction-test校驗(yàn):數(shù)字必須為${min}為${max}之間!</message>
</field-validator>
</field>
</validators>
二.我們一起看一下OGNL常用表達(dá)式:
1. 當(dāng)使用OGNL調(diào)用靜態(tài)方法的時(shí)候,需要按照如下語(yǔ)法編寫(xiě)表達(dá)式:
@package.classname@methodname(parameter)
2. 對(duì)于OGNL來(lái)說(shuō),java.lang.Math是其的默認(rèn)類(lèi),如果調(diào)用java.lang.Math的靜態(tài)方法時(shí),無(wú)需指定類(lèi)的名字,比如:@@min(4, 10);
3. 對(duì)于OGNL來(lái)說(shuō),數(shù)組與集合是一樣的,都是通過(guò)下標(biāo)索引來(lái)去訪問(wèn)的。
獲取List:<s:property value="testList"/><br>
獲取List中的某一個(gè)元素(可以使用類(lèi)似于數(shù)組中的下標(biāo)獲取List中的內(nèi)容):<s:property value="testList[0]"/><br>
獲取Set:<s:property value="testSet"/><br>
獲取Set中的某一個(gè)元素(Set由于沒(méi)有順序,所以不能使用下標(biāo)獲取數(shù)據(jù)):<s:property value="testSet[0]"/><br> ×
獲取Map:<s:property value="testMap"/><br>
獲取Map中所有的鍵:<s:property value="testMap.keys"/><br>
獲取Map中所有的值:<s:property value="testMap.values"/><br>
獲取Map中的某一個(gè)元素(可以使用類(lèi)似于數(shù)組中的下標(biāo)獲取List中的內(nèi)容):
<s:property value="testMap['m1']"/><br>
獲取List的大小:<s:property value="testSet.size"/><br>
4. 使用OGNL來(lái)處理映射(Map)的語(yǔ)法格式如下所示:
#{‘key1': ‘value1', ‘key2': ‘value2', ‘key3': ‘value3'};
5. 過(guò)濾(filtering):collection.{? expression}
6. OGNL針對(duì)集合提供了一些偽屬性(如size,isEmpty),讓我們可以通過(guò)屬性的方式來(lái)調(diào)用方法(本質(zhì)原因在于集合當(dāng)中的很多方法并不符合JavaBean的命名規(guī)則),但我么你依然還可以通過(guò)調(diào)用方法來(lái)實(shí)現(xiàn)與偽屬性相同的目的。
7. 過(guò)濾(filtering),獲取到集合中的第一個(gè)元素:collection.{^ expression}
8. 過(guò)濾(filtering),獲取到集合中的最后一個(gè)元素:collection.{& expression}
9. 在使用過(guò)濾操作時(shí),我們通常都會(huì)使用#this,該表達(dá)式用于代表當(dāng)前正在迭代的集合中的對(duì)象(聯(lián)想增強(qiáng)的for循環(huán))
10. 投影(projection):collection.{expression}
11. 過(guò)濾與投影之間的差別:類(lèi)比于數(shù)據(jù)庫(kù)中的表,過(guò)濾是取行的操作,而投影是取列的操作。 具體舉例如下:
利用選擇獲取List中成績(jī)及格的對(duì)象:<s:property value="stus.{?#this.grade>=60}"/><br>
利用選擇獲取List中成績(jī)及格的對(duì)象的username:
<s:property value="stus.{?#this.grade>=60}.{username}"/><br>
利用選擇獲取List中成績(jī)及格的第一個(gè)對(duì)象的username:
<s:property value="stus.{?#this.grade>=60}.{username}[0]"/><br>
利用選擇獲取List中成績(jī)及格的第一個(gè)對(duì)象的username:
<s:property value="stus.{^#this.grade>=60}.{username}"/><br>
利用選擇獲取List中成績(jī)及格的最后一個(gè)對(duì)象的username:
<s:property value="stus.{$#this.grade>=60}.{username}"/><br>
利用選擇獲取List中成績(jī)及格的第一個(gè)對(duì)象然后求大小:
<s:property value="stus.{^#this.grade>=600}.{username}.size"/><br>
12. 在Struts2中,根對(duì)象就是ValueStack。在Struts2的任何流程當(dāng)中,ValueStack中的最頂層對(duì)象一定是Action對(duì)象。
13. parameters,#parameters.username
request, #request.username
session, #session.username
application, #application.username
attr, #attr.username
以上幾個(gè)對(duì)象叫做“命名對(duì)象”。
14. 訪問(wèn)靜態(tài)方法或是靜態(tài)成員變量的改進(jìn)。
@vs@method
15. 關(guān)于Struts2標(biāo)簽庫(kù)屬性值的%與#號(hào)的關(guān)系:
1). 如果標(biāo)簽的屬性值是OGNL表達(dá)式,那么無(wú)需加上%{}。
2). 如果標(biāo)簽的屬性值是字符串類(lèi)型,那么在字符串當(dāng)中凡是出現(xiàn)的%{}都會(huì)被解析成OGNL表達(dá)式,解析完畢后再與其他的字符串進(jìn)行拼接構(gòu)造出最后的字符串值。
3). 我們可以在所有的屬性值上加%{},這樣如果該屬性值是OGNL表達(dá)式,那么標(biāo)簽處理類(lèi)就會(huì)將%{}忽略掉。
最后一起用代碼說(shuō)話,簡(jiǎn)單的看一下ognl操作的示例:
1)上下文環(huán)境中使用OGNL
public static void main(String[] args)
{
Map<String , Object> context = new HashMap<String , Object>();
Person person1 = new Person();
person1.setName("zhangsan");
Person person2 = new Person();
person2.setName("lisi");
Person person3 = new Person();
person3.setName("wangwu");
Person person4 = new Person();
person4.setName("zhaoliu");
context.put("person1", person1);
context.put("person2", person2);
context.put("person3", person3);
try
{
Object value = Ognl.getValue("name", context, person2);
System.out.println("ognl expression \"name\" evaluation is : " + value);
Object value2 = Ognl.getValue("#person2.name", context, person2);
System.out.println("ognl expression \"#person2.name\" evaluation is : " + value2);
Object value3 = Ognl.getValue("#person1.name", context, person2);
System.out.println("ognl expression \"#person1.name\" evaluation is : " + value3);
Object value4 = Ognl.getValue("name", context, person4);
System.out.println("ognl expression \"name\" evaluation is : " + value4);
Object value5 = Ognl.getValue("#person4.name", context, person4);
System.out.println("ognl expression \"person4.name\" evaluation is : " + value5);
// Object value6 = Ognl.getValue("#person4.name", context, person2);
// System.out.println("ognl expression \"#person4.name\" evaluation is : " + value6);
}
2)使用OGNL調(diào)用方法
public static void main(String[] args)
{
OgnlContext context = new OgnlContext();
People people1 = new People();
people1.setName("zhangsan");
People people2 = new People();
people2.setName("lisi");
People people3 = new People();
people3.setName("wangwu");
context.put("people1", people1);
context.put("people2", people2);
context.put("people3", people3);
context.setRoot(people1);
try
{
Object value = Ognl.getValue("name.length()", context, context.getRoot());
System.out.println("people1 name length is :" + value);
Object upperCase = Ognl.getValue("#people2.name.toUpperCase()", context, context.getRoot());
System.out.println("people2 name upperCase is :" + upperCase);
Object invokeWithArgs = Ognl.getValue("name.charAt(5)", context, context.getRoot());
System.out.println("people1 name.charAt(5) is :" + invokeWithArgs);
Object min = Ognl.getValue("@java.lang.Math@min(4,10)", context, context.getRoot());
System.out.println("min(4,10) is :" + min);
Object e = Ognl.getValue("@java.lang.Math@E", context, context.getRoot());
System.out.println("E is :" + e);
}
3)使用OGNL操作集合
public static void main(String[] args) throws Exception
{
OgnlContext context = new OgnlContext();
Classroom classroom = new Classroom();
classroom.getStudents().add("zhangsan");
classroom.getStudents().add("lisi");
classroom.getStudents().add("wangwu");
classroom.getStudents().add("zhaoliu");
classroom.getStudents().add("qianqi");
Student student = new Student();
student.getContactWays().put("homeNumber", "110");
student.getContactWays().put("companyNumber", "119");
student.getContactWays().put("mobilePhone", "112");
context.put("classroom", classroom);
context.put("student", student);
context.setRoot(classroom);
Object collection = Ognl.getValue("students", context, context.getRoot());
System.out.println("students collection is :" + collection);
Object firstStudent = Ognl.getValue("students[0]", context, context.getRoot());
System.out.println("first student is : " + firstStudent);
Object size = Ognl.getValue("students.size()", context, context.getRoot());
System.out.println("students collection size is :" + size);
System.out.println("--------------------------飄逸的分割線--------------------------");
Object mapCollection = Ognl.getValue("#student.contactWays", context, context.getRoot());
System.out.println("mapCollection is :" + mapCollection);
Object firstElement = Ognl.getValue("#student.contactWays['homeNumber']", context, context.getRoot());
System.out.println("the first element of contactWays is :" + firstElement);
System.out.println("--------------------------飄逸的分割線--------------------------");
Object createCollection = Ognl.getValue("{'aa','bb','cc','dd'}", context, context.getRoot());
System.out.println(createCollection);
Object createMapCollection = Ognl.getValue("#{'key1':'value1','key2':'value2'}", context, context.getRoot());
System.out.println(createMapCollection);
}
}
總結(jié)
以上就是本文關(guān)于OGNL表達(dá)式基本語(yǔ)法與用法詳解的全部?jī)?nèi)容,希望對(duì)大家有所幫助,有問(wèn)題的話可以隨時(shí)留言,小編會(huì)及時(shí)回復(fù)大家的。感興趣的朋友可以繼續(xù)參閱:Struts2 通過(guò)ognl表達(dá)式實(shí)現(xiàn)投影 、 Struts2 OGNL表達(dá)式實(shí)例詳解等。
相關(guān)文章
詳解Java中的時(shí)間處理與時(shí)間標(biāo)準(zhǔn)
這篇文章主要為大家詳細(xì)介紹了三個(gè)時(shí)間標(biāo)準(zhǔn)GMT,CST,UTC的規(guī)定,以及Java進(jìn)行時(shí)間處理的相關(guān)知識(shí),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-09-09
SpringBoot整合BootStrap實(shí)戰(zhàn)
這篇文章主要介紹了SpringBoot整合BootStrap實(shí)戰(zhàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
簡(jiǎn)單談?wù)凧ava遍歷樹(shù)深度優(yōu)先和廣度優(yōu)先的操作方式
這篇文章主要介紹了簡(jiǎn)單談?wù)凧ava遍歷樹(shù)深度優(yōu)先和廣度優(yōu)先的操作方式的相關(guān)資料,需要的朋友可以參考下2023-03-03
shuffle的關(guān)鍵階段sort(Map端和Reduce端)源碼分析
今天小編就為大家分享一篇關(guān)于shuffle的關(guān)鍵階段sort(Map端和Reduce端)源碼分析,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01
IDEA集成Docker實(shí)現(xiàn)快捷部署的操作步驟
這篇文章主要介紹了IDEA集成Docker實(shí)現(xiàn)快捷部署的操作步驟,通過(guò)靈活利用這一功能,開(kāi)發(fā)人員可以更快速地開(kāi)發(fā)、調(diào)試和部署應(yīng)用程序,從而提高開(kāi)發(fā)工作的效率和質(zhì)量,需要的朋友可以參考下2024-06-06
spring?security需求分析與基礎(chǔ)環(huán)境準(zhǔn)備教程
這篇文章主要為大家介紹了spring?security需求分析與基礎(chǔ)環(huán)境準(zhǔn)備教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03
使用RequestBodyAdvice實(shí)現(xiàn)對(duì)Http請(qǐng)求非法字符過(guò)濾
這篇文章主要介紹了使用RequestBodyAdvice實(shí)現(xiàn)對(duì)Http請(qǐng)求非法字符過(guò)濾的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06

