1、OGNL表達式
(1)概念
OGNL:對象導航圖語言(Object Graph Navigation Language),是一種表達式語言,功能比EL表達式更為強大,它是集成在Struts中的。
在創建Struts項目的時候已經將OGNL有關的包導入了,所以,這里不需要重復導包。
(2)OGNLContext對象:
EL表達式從是一個內置對象中取值,而OGNL表達式只從OGNLContext對象中取值,該對象可以分為兩部分,其中root部分可以存放任何對象,Context部分只能存放鍵值對。
2、OGNL初始化和取值
public class OgnlTest { public void test() throws OgnlException { User rootuser=new User("zhai","123",12);//root部分 Map<String,User> context=new HashMap<String, User>();//context部分 context.put("user1",new User("user1","111",12)); context.put("user2",new User("user2","222",13)); OgnlContext ognlContext=new OgnlContext();//創建OGNLContext對象 ognlContext.setRoot(rootuser); ognlContext.setValues(context);//將root和context部分放入到OGNLContext內部 String name= (String)Ognl.getValue("username",ognlContext,ognlContext.getRoot());//取值 Integer age=(Integer)Ognl.getValue("userage",ognlContext,ognlContext.getRoot()); String password=(String)Ognl.getValue("password",ognlContext,ognlContext.getRoot()); System.out.println("用戶名:"+name+",年齡"+age+",密碼:"+password); String name1= (String)Ognl.getValue("#user1.username",ognlContext,ognlContext.getRoot()); Integer age1=(Integer)Ognl.getValue("#user2.userage",ognlContext,ognlContext.getRoot()); System.out.println("用戶名:"+name1+",年齡"+age1); }
(1)在初始化部分,需要先對root和context分別做初始化操作,然后將root和context放入到OGNLContext對象內部,這樣初始化工作就完成了。
(2)取值操作分為兩種:從root中取值和從context中取值,從root中取值的時候不需要加“#”,只需要寫入變量名即可;當從context中取值的時候需要先加上“#”。
例如:"#user1.username"中user1的作用是取出鍵為user1的對象,即:new User("user1","111",12)這一部分,表達式#user1.username,則是從對象中取出對象的username屬性的值。
3、OGNL表達式的賦值
public void test1() throws OgnlException { User rootuser=new User("zhai","123",12); Map<String,User> context=new HashMap<String, User>(); context.put("user1",new User("user1","111",12)); context.put("user2",new User("user2","222",13)); OgnlContext ognlContext=new OgnlContext(); ognlContext.setRoot(rootuser); ognlContext.setValues(context);
Ognl.getValue("username='zhang'",ognlContext,ognlContext.getRoot()); String name= (String)Ognl.getValue("username",ognlContext,ognlContext.getRoot()); System.out.println("用戶名:"+name); Ognl.getValue("#user1.username='zhao'",ognlContext,ognlContext.getRoot()); String name1= (String)Ognl.getValue("#user1.username",ognlContext,ognlContext.getRoot()); System.out.println("用戶名:"+name1); }
用getValue()方法對元素的值進行修改,需要注意對root和context中的變量進行賦值的不同點。
4、調用方法
(1)root:
public void test2() throws OgnlException { User rootuser=new User("zhai","123",12); Map<String,User> context=new HashMap<String, User>(); context.put("user1",new User("user1","111",12)); context.put("user2",new User("user2","222",13)); OgnlContext ognlContext=new OgnlContext(); ognlContext.setRoot(rootuser); ognlContext.setValues(context); Ognl.getValue("setUsername('ZHAI')",ognlContext,ognlContext.getRoot()); String name=(String) Ognl.getValue("getUsername()",ognlContext,ognlContext.getRoot()); System.out.println("用戶名:"+name); }
先動用User類中user對象的set方法,對user對象的username值進行修改,修改后在調用get方法取出對象的值。
(2)context
public void test2() throws OgnlException { User rootuser=new User("zhai","123",12); Map<String,User> context=new HashMap<String, User>(); context.put("user1",new User("user1","111",12)); context.put("user2",new User("user2","222",13)); OgnlContext ognlContext=new OgnlContext(); ognlContext.setRoot(rootuser); ognlContext.setValues(context); Ognl.getValue("#user1.setUsername('ZHAI')",ognlContext,ognlContext.getRoot()); String name=(String) Ognl.getValue("#user1.getUsername()",ognlContext,ognlContext.getRoot()); System.out.println("用戶名:"+name); }
先用“#user1”調用對象user1,然后對user1對象進行賦值,在進行取值。
(3)調用自定義的靜態方法:
先創建一個靜態方法:
public class Hello { public static String nihao(String name){ return "你好,"+name; } }
用OGNL表達式調用靜態方法:
public void test3() throws OgnlException { User rootuser=new User("zhai","123",12); Map<String,User> context=new HashMap<String, User>(); context.put("user1",new User("user1","111",12)); context.put("user2",new User("user2","222",13)); OgnlContext ognlContext=new OgnlContext(); ognlContext.setRoot(rootuser); ognlContext.setValues(context); String string=(String) Ognl.getValue("@pers.zhb.ognl.Hello@nihao('zhai')",ognlContext,ognlContext.getRoot()); System.out.println(string); }
關鍵點是利用完整類名和靜態方法名實現對方法的調用。
(4)調用已有的靜態方法或屬性:
public void test3() throws OgnlException { User rootuser=new User("zhai","123",12); Map<String,User> context=new HashMap<String, User>(); context.put("user1",new User("user1","111",12)); context.put("user2",new User("user2","222",13)); OgnlContext ognlContext=new OgnlContext(); ognlContext.setRoot(rootuser); ognlContext.setValues(context); Double aDouble= (Double) Ognl.getValue("@java.lang.Math@PI",ognlContext,ognlContext.getRoot()); System.out.println(aDouble); }
和調用自定義的靜態方法或屬性一樣,只需完整類名和方法或屬性名即可。
5、集合對象
(1)list集合:
public void test4() throws OgnlException {
User rootuser=new User("zhai","123",12); Map<String,User> context=new HashMap<String, User>(); context.put("user1",new User("user1","111",12)); context.put("user2",new User("user2","222",13)); OgnlContext ognlContext=new OgnlContext(); ognlContext.setRoot(rootuser); ognlContext.setValues(context); String string1=(String) Ognl.getValue("{'wu','han','jia','you'}[1]",ognlContext,ognlContext.getRoot()); String string2=(String) Ognl.getValue("{'wu','han','jia','you'}.get(1)",ognlContext,ognlContext.getRoot()); System.out.println(string1); System.out.println(string2); }
{'wu','han','jia','you'}為創建的一個list集合對象,以上程序為分別用兩種方式獲取list集合中的元素。
(2)map集合:
public void test5() throws OgnlException {
User rootuser=new User("zhai","123",12); Map<String,User> context=new HashMap<String, User>(); context.put("user1",new User("user1","111",12)); context.put("user2",new User("user2","222",13)); OgnlContext ognlContext=new OgnlContext(); ognlContext.setRoot(rootuser); ognlContext.setValues(context); String string1=(String) Ognl.getValue("#{'name':'zhao','age':12}['name']",ognlContext,ognlContext.getRoot()); System.out.println(string1); }
與list集合不同,創建時需要在集合前面加上“#”,用鍵取值。