OGNL學習(一)Ognl和OgnlContext

來源:互聯網
上載者:User

各種運算式如下:(匯入相關jar包,OGNL.jar 和javassist.jar)

package com.ognl;import java.util.ArrayList;import java.util.List;import ognl.Ognl;import ognl.OgnlContext;public class OgnlTest {public static void main(String[] args) throws Exception  {Person person = new Person();person.setName("zhangsan");Dog dog = new Dog();dog.setName("wangcai");Dog dog2 = new Dog();dog2.setName("maomao");person.setDog(dog2);OgnlContext context = new OgnlContext();//實現了map介面context.put("person", person);context.put("dog", dog);context.setRoot(person);Object object = Ognl.parseExpression("name");System.out.println(object);//name/* * Ognl.getValue(object,context,context.getRoot()) * 它會到指定的上下文context中去尋找object,預設是到根項目中尋找 * 在OGNL中,如果運算式沒有使用#號,那麼OGNL會從根對象中尋找該屬性對應的get() * 方法,如果尋找的不是根對象的中的屬性,那麼需要以#開頭,告訴OGNL,去尋找指定對象中的屬性 */Object object2 = Ognl.getValue(object, context,context.getRoot());System.out.println(object2);//zhangsanObject o = Ognl.getValue("name", context,context.getRoot());System.out.println(o);//zhangsanObject object3 = Ognl.getValue("#dog.name",context,context.getRoot());System.out.println(object3);//wangcaiObject object4 = Ognl.getValue("#person.dog.name", context,context.getRoot());System.out.println(object4);//maomao/* * 調用屬性所用的方法 */System.out.println("-------------------------------");//Object obj = Ognl.getValue("getName().toUpperCase()", context,context.getRoot());//效果同下Object object5 = Ognl.getValue("name.toUpperCase()", context,context.getRoot());System.out.println(object5);//ZHANGSANSystem.out.println("--------------------------------");/* * 當使用OGNL調用靜態方法的時候,需要按照如下的文法編寫運算式 * @package.classname@method(parameters) * 較特殊的類:對OGNL來說,java.lang.Math是其預設的類,即調用java.lang.Math的靜態方法時, * 無需指定類的名稱 */Object object6 = Ognl.getValue("@java.lang.Integer@toBinaryString(10)",context,context.getRoot());System.out.println(object6);//1010Object object7 = Ognl.getValue("@@min(7,10)", context,context.getRoot());System.out.println(object7);//7System.out.println("----------------------------");/* * 數組和集合 * 都是通過下標索引訪問 */Object object8 = Ognl.getValue("{'aa','bb','cc'}[2]", context,context.getRoot());System.out.println(object8);//ccSystem.out.println("-------------------------");/* * 映射Map * 文法格式如下: * #{'key1':'value1','key2':'value2'} */Object object9 = Ognl.getValue("#{'key1':'value1','key2':'value2'}['key2']", context,context.getValues());System.out.println(object9);//value2System.out.println("--------------------------");/* * 過濾 * 格式: * collection.{? expression} * 返回的是集合 * #this表示遍曆時每個對象(聯想增強for迴圈) */List<Person> list = new ArrayList<Person>();Person p1 = new Person();Person p2 = new Person();Person p3 = new Person();p1.setName("hehe");p2.setName("xixi3");p3.setName("haha333");list.add(p1);list.add(p2);list.add(p3);context.put("list",list);System.out.println(Ognl.getValue("#list.{? #this.name.length() > 4}.size()", context,context.getRoot()));//2/* * 過濾擷取集合中的第一個元素 * 運算式: * collection.{^ expression} * 傳回值為集合 */System.out.println(Ognl.getValue("#list.{^ #this.name.length() > 4}.get(0).getName()", context,context.getRoot()));//xixi3/* * 過濾器取得集合中最後一個元素 * 運算式: * collection.{$ expression} */System.out.println(Ognl.getValue("#list.{$ #this}[0].name", context,context.getRoot()));//haha333/* * 投影 * 運算式: * collection.{expression} * 返回集合,返回集合長度和原來集合的長度相等 * 返回的集合中的對象是原來對象的子集 *  * 過濾與投影的區別: * 類比於資料庫中的表,過濾是取行,而投影是取列的操作 */System.out.println(Ognl.getValue("#list.{name}", context,context.getValues()));//[hehe, xixi3, haha333]System.out.println(Ognl.getValue("#list.{#this.name.length()>4?'Hello world':#this.name}", context,context.getRoot()));//[hehe, Hello world, Hello world]}}

 在Struts2中,根對象就是ValueStack(一個堆棧),在Struts2的任何流程中,ValueStack中的最頂層對象一定是Action對象。

以下幾個對象,叫做“命名物件” ,不在ValueStack中:

 

類名 訪問方式
parameters(用戶端傳向伺服器端的參數) #parameters.uernme
request #request.username
application

#application.username

訪問 靜態方法或靜態成員變數的改進@vs@method(其中vs表示ValueStack)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.