Struts2之Ognl
上一篇算是看著學習了下ValueStack、ActionContext,如果還沒明白他們之間是怎么回事,沒關系,可以先學下Ognl,然后再回頭看前面的可能就比較好理解了,我昨天也是一頭霧水,現(xiàn)在把Ognl的學習了下,慢慢的有了感覺。
一、概念
OGNL表達式是(Object-Graph?Navigation?Language)是對象圖形化導航語言。OGNL是一個開源的項目,struts2中默認使用OGNL表達式語言來顯示數(shù)據(jù)。與serlvet中的el表達式的作用是一樣的。OGNL表達式有下面以下特點:
1.支持對象方法調(diào)用,例如:objName.methodName();
2.支持類靜態(tài)的方法調(diào)用和值訪問,表達式的格式為
???? @[類全名(包括包路經(jīng))]
???? @[方法名?|??值名]
???? 例如:
???????? @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.操作集合對象
二、實踐
這里定義了Address、User類
package com.cyw.test;public class Address {public Address(String city, String street) {super();this.city = city;this.street = street;}private String city;public String getCity() {return city;}public void setCity(String city) {this.city = city;}public String getStreet() {return street;}public void setStreet(String street) {this.street = street;}private String street; } View Code package com.cyw.test;import java.util.Date;public class User {private String name;private int age;private Date birthDay;private Address address;public User(String name, int age, Date birthDay, Address address) {super();this.name = name;this.age = age;this.birthDay = birthDay;this.address = address;}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address;}public Date getBirthDay() {return birthDay;}public void setBirthDay(Date birthDay) {this.birthDay = birthDay;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}} View Code Address address=new Address("深圳市","坂田新天下");User user=new User("cyw",25,new Date(),address);//獲取javabean的屬性值String name=(String) Ognl.getValue("name", new HashMap(), user) ;System.out.println(name);//獲取對象的屬性值String city=(String) Ognl.getValue("address.city", new HashMap(), user) ;System.out.println(city);//修改屬性值name=(String) Ognl.getValue("name='cuiyw'", new HashMap(), user) ;System.out.println(name);Ognl.getValue("setName('cyw')", new HashMap(), user) ;//調(diào)用對象方法name=(String) Ognl.getValue("getName()", new HashMap(), user) ;System.out.println(name);//訪問靜態(tài)方法 格式:@包名+類名@靜態(tài)方法UUID randomUUID=(UUID)Ognl.getValue("@java.util.UUID@randomUUID()", new HashMap(), user) ;System.out.println(randomUUID.toString());//訪問靜態(tài)變量格式:@包名+類名@靜態(tài)變量名double pi=(double)Ognl.getValue("@java.lang.Math@PI", new HashMap(), user);System.out.println(pi);//操作索引數(shù)組元素int []array={1,2,3,4};int first= (int) Ognl.getValue("[0]", new HashMap(), array);System.out.println(first);//操作集合List<User> userList=new ArrayList<User>();userList.add(user);name = (String) Ognl.getValue("[0].name", new HashMap(), userList);System.out.println(name);//操作MapMap<String,String> map=new HashMap<String,String>();map.put("key1", "cui");map.put("key2", "yan");name = (String) Ognl.getValue("key1", new HashMap(), map);System.out.println(name);//創(chuàng)建listList<String> list=(List<String>)Ognl.getValue("{'cui','yan'}", new HashMap(), new Object());System.out.println(list.get(0));//創(chuàng)建mapmap=(Map)Ognl.getValue("#{'name':'cui','name2':'yan'}", new HashMap(), new Object());System.out.println(map.get("name"));三、ActionContext、ValueStack
ActionContext:
OGNL中的上下文即struts2中的actionContext,OGNL中的root即struts2中的valueStack。
充當OGNL的context,是action的上下文,也可以叫做action的數(shù)據(jù)中心,本質(zhì)是一個map,在其中,所有的數(shù)據(jù)都存放在這里,那其中到底存放了哪些東西呢,actionContext中存放數(shù)據(jù)的方式又是怎樣的?
actionContext是一個map,所以其中都是以鍵值對的形式存儲對象,如下圖所示,
request、session、application這種我們熟知的作用域,注意是作用域,而不是對象,
paramters:這個是表單提交的參數(shù),全部都會放到這個map中,
attr(attributes):三個作用域所有的屬性都會放在該map下,如果有重復的,那么以request域中的為準。
VALUE_STACK:值棧,存放著valueStack對象,也就是說,通過ActionContext能夠獲取到valueStack。
如果我們使用actionContext.put();? 那么會將該鍵值對直接放入到ActionContext下
ValueStack:值棧,本質(zhì)是一個ArrayList,作用,充當ognl的root,給一次請求中共享數(shù)據(jù)的功能。
root:源碼中的名稱為CompoundRoot,它也是一個棧,而每次值棧中入棧和出棧等操作其實就是對CompoundRoot進行對應的操作。
Context:對actionContext的引用,也就是通過valueStack也能夠獲取到上下文,通過getContext();
在我們訪問一個action時,會將action加入到棧頂,也就是action會在CompoundRoot的棧頂,而我們提交的各種表單參數(shù)(充當了ognl表達式)會在valueStack從頂向下查找對應的屬性進行賦值。這就是值棧的作用。
參考:http://blog.csdn.net/v123411739/article/details/24052989
轉(zhuǎn)載于:https://www.cnblogs.com/5ishare/p/6636769.html
總結
以上是生活随笔為你收集整理的Struts2之Ognl的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (解决)mysql1366中文显示错误的
- 下一篇: 如何自动化安装字体(命令行批量)