Struts2中OGNL表達式的原理是什么

Struts2中OGNL表達式的原理是什么,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

目前創(chuàng)新互聯(lián)已為上1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、綿陽服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計、鹽湖網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

一、OGNL表達式基礎(chǔ)知識 

二、OGNL與Struts2

OGNL表達式

OGNL,全稱為Object-Graph Navigation Language,它是一個功能強大的表達式語言,用來獲取和設(shè)置Java對象的屬性,它旨在提供一個更高的更抽象的層次來對Java對象圖進行導(dǎo)航。

OGNL表達式的基本單位是"導(dǎo)航鏈",一般導(dǎo)航鏈由如下幾個部分組成:

  1. 屬性名稱(property) 

  2. 方法調(diào)用(method invoke) 

  3. 數(shù)組元素

所有的OGNL表達式都基于當前對象的上下文來完成求值運算,鏈的前面部分的結(jié)果將作為后面求值的上下文。例如:names[0].length()。

示例:第一個OGNL程序

public class OGNL1  {      public static void main(String[] args)      {          /* 創(chuàng)建一個Person對象 */         Person person = new Person();          person.setName("zhangsan");                    try         {              /* 從person對象中獲取name屬性的值 */             Object value = Ognl.getValue("name", person);               System.out.println(value);          }          catch (OgnlException e)          {              e.printStackTrace();          }      }  }   class Person  {      private String name;       public String getName()      {          return name;      }       public void setName(String name)      {          this.name = name;      }  }

控制臺輸出:

zhangsan

可以看到我們正確的取得了person對象的name屬性值,該getValue聲明如下:

public static <T> T getValue(String expression,Object root)throws OgnlException   Convenience method that combines calls to  parseExpression  and  getValue.    Parameters:  expression - the OGNL expression to be parsed  root - the root object for the OGNL expression   Returns:  the result of evaluating the expression

OGNL會根據(jù)表達式從根對象(root)中提取值。

示例:上下文環(huán)境中使用OGNL

public class OGNL1  {      public static void main(String[] args)      {          /* 創(chuàng)建一個上下文Context對象,它是用保存多個對象一個環(huán)境 對象 */         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");           /* person4不放入到上下文環(huán)境中 */         Person person4 = new Person();          person4.setName("zhaoliu");           /* 將person1、person2、person3添加到環(huán)境中(上下文中) */         context.put("person1", person1);          context.put("person2", person2);          context.put("person3", person3);           try         {              /* 獲取根對象的"name"屬性值 */             Object value = Ognl.getValue("name", context, person2);              System.out.println("ognl expression \"name\" evaluation is : " + value);               /* 獲取根對象的"name"屬性值 */             Object value2 = Ognl.getValue("#person2.name", context, person2);              System.out.println("ognl expression \"#person2.name\" evaluation is : " + value2);               /* 獲取person1對象的"name"屬性值 */             Object value3 = Ognl.getValue("#person1.name", context, person2);              System.out.println("ognl expression \"#person1.name\" evaluation is : " + value3);               /* 將person4指定為root對象,獲取person4對象的"name"屬性,注意person4對象不在上下文中 */             Object value4 = Ognl.getValue("name", context, person4);              System.out.println("ognl expression \"name\" evaluation is : " + value4);               /* 將person4指定為root對象,獲取person4對象的"name"屬性,注意person4對象不在上下文中 */             Object value5 = Ognl.getValue("#person4.name", context, person4);              System.out.println("ognl expression \"person4.name\" evaluation is : " + value5);               /* 獲取person4對象的"name"屬性,注意person4對象不在上下文中 */             // Object value6 = Ognl.getValue("#person4.name", context, person2);              // System.out.println("ognl expression \"#person4.name\" evaluation is : " + value6);           }          catch (OgnlException e)          {              e.printStackTrace();          }      }  }   class Person  {      private String name;       public String getName()      {          return name;      }       public void setName(String name)      {          this.name = name;      }  }

控制臺輸出:

ognl expression "name" evaluation is : lisi  ognl expression "#person2.name" evaluation is : lisi  ognl expression "#person1.name" evaluation is : zhangsan  ognl expression "name" evaluation is : zhaoliu  ognl.OgnlException: source is null for getProperty(null, "name")      at ognl.OgnlRuntime.getProperty(OgnlRuntime.java:2296)      at ognl.ASTProperty.getValueBody(ASTProperty.java:114)      at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)      at ognl.SimpleNode.getValue(SimpleNode.java:258)      at ognl.ASTChain.getValueBody(ASTChain.java:141)      at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)      at ognl.SimpleNode.getValue(SimpleNode.java:258)      at ognl.Ognl.getValue(Ognl.java:494)      at ognl.Ognl.getValue(Ognl.java:596)      at ognl.Ognl.getValue(Ognl.java:566)      at com.beliefbetrayal.ognl.OGNL1.main(OGNL1.java:53)

對于使用上下文的OGNL,若不指定從哪一個對象中查找"name"屬性,則OGNL直接從根對象(root)查找,若指定查找對象(使用'#'號指定,如#person1),則從指定的對象中查找,若指定對象不在上下文中則會拋出異常,換句話說就是是#person1.name形式指定查找對象則必須要保證指定對象在上下文環(huán)境中。

示例:使用OGNL調(diào)用方法

public class OGNL2  {      public static void main(String[] args)      {          /* OGNL提供的一個上下文類,它實現(xiàn)了Map接口 */         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         {              /* 調(diào)用 成員方法 */             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);               /* 調(diào)用靜態(tài)方法 */             Object min = Ognl.getValue("@java.lang.Math@min(4,10)", context, context.getRoot());              System.out.println("min(4,10) is :" + min);               /* 調(diào)用靜態(tài)變量 */             Object e = Ognl.getValue("@java.lang.Math@E", context, context.getRoot());              System.out.println("E is :" + e);          }          catch (OgnlException e)          {              e.printStackTrace();          }      }  }   class People  {      private String name;       public String getName()      {          return name;      }       public void setName(String name)      {          this.name = name;      }  }

控制臺輸出:

people1 name length is :8 people2 name upperCase is :LISI  people1 name.charAt(5) is :s  min(4,10) is :4 E is :2.718281828459045

使用OGNL調(diào)用方法也十分簡單,對于成員方法調(diào)用,只需要給出方法的名稱+(),若有參數(shù),直接寫在括號內(nèi),與一般調(diào)用Java方法一致。對于靜態(tài)方法的調(diào)用,需要使用如下格式:@ClassName@method,對于靜態(tài)變量需要使用如下格式:@ClassName@field。

示例:使用OGNL操作集合

public class OGNL3  {      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);           /* 獲得classroom的students集合 */         Object collection = Ognl.getValue("students", context, context.getRoot());          System.out.println("students collection is :" + collection);           /* 獲得classroom的students集合 */         Object firstStudent = Ognl.getValue("students[0]", context, context.getRoot());          System.out.println("first student is : " + firstStudent);           /* 調(diào)用集合的方法 */         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("--------------------------飄逸的分割線--------------------------");           /* 創(chuàng)建集合 */         Object createCollection = Ognl.getValue("{'aa','bb','cc','dd'}", context, context.getRoot());          System.out.println(createCollection);           /* 創(chuàng)建Map集合 */         Object createMapCollection = Ognl.getValue("#{'key1':'value1','key2':'value2'}", context, context.getRoot());          System.out.println(createMapCollection);       }  }   class Classroom  {      private List<String> students = new ArrayList<String>();       public List<String> getStudents()      {          return students;      }       public void setStudents(List<String> students)      {          this.students = students;      }  }   class Student  {      private Map<String , Object> contactWays = new HashMap<String , Object>();       public Map<String , Object> getContactWays()      {          return contactWays;      }       public void setContactWays(Map<String , Object> contactWays)      {          this.contactWays = contactWays;      }  }

控制臺的輸出:

students collection is :[zhangsan, lisi, wangwu, zhaoliu, qianqi]  first student is : zhangsan  students collection size is :5 --------------------------飄逸的分割線--------------------------  mapCollection is :{homeNumber=110, mobilePhone=112, companyNumber=119}  the first element of contactWays is :110 --------------------------飄逸的分割線--------------------------  [aa, bb, cc, dd]  {key1=value1, key2=value2}

OGNL不僅可以操作集合對象,還可以創(chuàng)建集合對象,對集合操作與對屬性的操作沒什么不同,需要注意的是OGNL認為List與Array是一樣的。使用OGNL創(chuàng)建List集合時使用{},創(chuàng)建Map對象時使用#{}。

示例:使用OGNL過濾集合與投影集合

public class OGNL4  {      public static void main(String[] args) throws Exception      {          OgnlContext context = new OgnlContext();           Humen humen = new Humen();          humen.setName("qiuyi");          humen.setSex("n");          humen.setAge(22);          humen.getFriends().add(new Humen("zhangsan" , "n" , 22));          humen.getFriends().add(new Humen("lisi" , "f" , 21));          humen.getFriends().add(new Humen("wangwu" , "n" , 23));          humen.getFriends().add(new Humen("zhaoliu" , "n" , 22));          humen.getFriends().add(new Humen("qianqi" , "n" , 22));          humen.getFriends().add(new Humen("sunba" , "f" , 20));          humen.getFriends().add(new Humen("yangqiu" , "f" , 25));                    context.put("humen", humen);          context.setRoot(humen);           /* OGNL過濾集合的語法為:collection.{? expression} */         Object filterCollection = Ognl.getValue("friends.{? #this.name.length() > 7}", context, context.getRoot());          System.out.println("filterCollection is :" + filterCollection);           System.out.println("--------------------------飄逸的分割線--------------------------");           /* OGNL投影集合的語法為:collection.{expression} */         Object projectionCollection = Ognl.getValue("friends.{name}", context, context.getRoot());          System.out.println("projectionCollection is :" + projectionCollection);      }  }   class Humen  {      private String name;      private String sex;      private int age;      private List<Humen> friends = new ArrayList<Humen>();       public Humen()      {       }       public Humen(String name , String sex , int age)      {          this.name = name;          this.sex = sex;          this.age = age;      }       public String getName()      {          return name;      }       public void setName(String name)      {          this.name = name;      }       public String getSex()      {          return sex;      }       public void setSex(String sex)      {          this.sex = sex;      }       public int getAge()      {          return age;      }       public void setAge(int age)      {          this.age = age;      }       public List<Humen> getFriends()      {          return friends;      }       public void setFriends(List<Humen> friends)      {          this.friends = friends;      }       @Override     public String toString()      {          return "Humen [name=" + name + ", sex=" + sex + ", age=" + age + "]";      }  }

控制臺輸出:

filterCollection is :[Humen [name=zhangsan, sex=n, age=22]]  --------------------------飄逸的分割線--------------------------  projectionCollection is :[zhangsan, lisi, wangwu, zhaoliu, qianqi, sunba, yangqiu]

OGNL可以對集合進行過濾與投影操作,過濾的語法為collection.{? expression},其中使用"#this"表示集合當前對象(可以與for-each循環(huán)比較)。投影的語法為collection.{expression}。投影和過濾可以看做是數(shù)據(jù)庫中對表取列和取行的操作。


Struts2與OGNL

Struts 2支持以下幾種表達式語言:
1. OGNL(Object-Graph Navigation Language),可以方便地操作對象屬性的開源表達式語言;
2. JSTL(JSP Standard Tag Library),JSP 2.0集成的標準的表達式語言;
3. Groovy,基于Java平臺的動態(tài)語言,它具有時下比較流行的動態(tài)語言(如Python、Ruby和Smarttalk等)的一些起特性;
4. Velocity,嚴格來說不是表達式語言,它是一種基于Java的模板匹配引擎,具說其性能要比JSP好。

Struts 2默認的表達式語言是OGNL,原因是它相對其它表達式語言具有下面幾大優(yōu)勢:
1. 支持對象方法調(diào)用,如xxx.doSomeSpecial();
2. 支持類靜態(tài)的方法調(diào)用和值訪問,表達式的格式為@[類全名(包括包路徑)]@[方法名 | 值名],例如:@java.lang.String@format('foo %s', 'bar')或@tutorial.MyConstant@APP_NAME;
3. 支持賦值操作和表達式串聯(lián),如price=100, discount=0.8, calculatePrice(),這個表達式會返回80;
4. 訪問OGNL上下文(OGNL context)和ActionContext;
5. 操作集合對象。

&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;以上內(nèi)容引用自http://www.blogjava.net/max/archive/2007/04/28/114417.html

平時使用Struts2標簽時會出現(xiàn)一些很奇特的問題,對于OGNL不了解的人可能對問題的出現(xiàn)無能為力或者就算解決了問題也不知道是如何解決的。下面總結(jié)一些使用Struts2標簽容易出現(xiàn)的困惑:

問題一:#,%{},$符號

在Struts2標簽屬性中經(jīng)常會出現(xiàn)"#"或者"%{}"的符號出現(xiàn),通過上面OGNL表達式基礎(chǔ)的介紹,知道了OGNL上下文中有且僅有一個根對象。Struts2為我們定義了許多明明對象,他們分別是"ValueStack","Parameters","Session","Request", "Appliction","Attr",其中"ValueStack"被設(shè)置為上下文的根對象。訪問非根對象必須加上"#"號,這就是出現(xiàn)"#"的原因。Struts2中的標的處理類,并不是所有都將標簽的屬性作為OGNL表達式來看待,有時候我們需要設(shè)置動態(tài)地值,則必須告訴標簽的處理類該字符串按照OGNL表達式來處理,%{}符號的作用就是告訴標簽的處理類將它包含的字符串按照OGNL表達式處理。 "$"符號用于XML文件中用于獲取動態(tài)值,與%{}作用類似。

問題二:%{}符號的影響

Struts2的標簽幾十幾百個,要記住哪一個標簽的處理類將標簽的屬性作為OGNL表達式是一件很困難的事情,在不清楚處理類的處理方式時怎么辦,%{}對于標簽處理類來說,若處理類將屬性值作為普通字符串則%{}符號包含的字符串當做OGNL表達式,若處理類將屬性值作為OGNL表達式來處理,則直接忽略%{}符號。換句話說,不清楚處理方式的話,可以都使用%{}符號。

問題三:標簽是如何獲得數(shù)據(jù)

下面是ValueStack的官方描述:

ValueStack allows multiple beans to be pushed in and dynamic EL expressions to be evaluated against it. When evaluating an expression, the stack will be searched down the stack, from the latest objects pushed in to the earliest, looking for a bean with a getter or setter for the given property or a method of the given name (depending on the expression being evaluated).

大致意思:ValueStack允許保存多個bean(也就是Action),并且可以使用表達式語言獲得他們。當評估一個表達式,ValueStack將會從棧頂?shù)綏5椎姆较虮凰阉饕槐?,對于給定的屬性名稱尋找bean的getter或setter方法或?qū)ふ医o定的方法。

Struts2中OGNL表達式的原理是什么

每當一個請求到達Action時,Struts2會將Action對象推入ValueStack中。

<body>       username:<s:property value="username"/><br />      -------------------詭異的分割線-------------------<br />      username:<%= ((HelloWorldAction)ActionContext.getContext().getValueStack().peek()).getUsername() %><br />    </body>

頁面顯示結(jié)果:

username:zhangsan  -------------------詭異的分割線-------------------  username:zhangsan

可以看到標簽取值與用Java代碼取值的結(jié)果相同,明顯標簽的取值方式更簡練簡潔。OGNL表達式"username"表示了從根對象ValueStack中取出屬性username的值。它會從棧頂?shù)綏5妆闅vValueStack,直到找某一個Action中的"username"屬性。

看完上述內(nèi)容,你們掌握Struts2中OGNL表達式的原理是什么的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

新聞標題:Struts2中OGNL表達式的原理是什么
網(wǎng)頁鏈接:http://muchs.cn/article26/ihiccg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計公司網(wǎng)站內(nèi)鏈、軟件開發(fā)、外貿(mào)網(wǎng)站建設(shè)用戶體驗、手機網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站