OGNLContext對象有兩部分構成
一部分是ROOT:可以放置任何對象作為ROOT
另外一部分Context:必須是Map形式(鍵值對)
OGNL表達式操作
package cn.future.a_ognl;import java.util.HashMap; import java.util.Map;import ognl.Ognl; import ognl.OgnlContext; import ognl.OgnlException;import org.junit.Test;import cn.future.domain.User;public class Demo {@Test//取出Root中的值public void fun() throws OgnlException{//OGNL表達式//準備ROOTUser userRoot = new User("ms",25);//準備ContextMap<String,User> contextMap = new HashMap<String, User>();contextMap.put("user1", new User("AAA",10));contextMap.put("user2", new User("BBB",11));//書寫OGNLOgnlContext oc = new OgnlContext();oc.setRoot(userRoot);oc.setValues(contextMap);//OGNL取值//取root中userRoot對象的name屬性String name = (String) Ognl.getValue("name", oc, oc.getRoot());int age = (Integer) Ognl.getValue("age", oc, oc.getRoot());System.out.println(name);System.out.println(age);}@Test//取出Context中的值public void fun1() throws OgnlException{//OGNL表達式//準備ROOTUser userRoot = new User("ms",25);//準備ContextMap<String,User> contextMap = new HashMap<String, User>();contextMap.put("user1", new User("AAA",10));contextMap.put("user2", new User("BBB",11));//書寫OGNLOgnlContext oc = new OgnlContext();oc.setRoot(userRoot);oc.setValues(contextMap);//OGNL取值//取User1對象的name屬性String name = (String) Ognl.getValue("#user1.name", oc, oc.getRoot());int age = (Integer) Ognl.getValue("#user1.age", oc, oc.getRoot());System.out.println(name);System.out.println(age);}@Test//為屬性賦值public void fun2() throws OgnlException{//OGNL表達式//準備ROOTUser userRoot = new User("ms",25);//準備ContextMap<String,User> contextMap = new HashMap<String, User>();contextMap.put("user1", new User("AAA",10));contextMap.put("user2", new User("BBB",11));//書寫OGNLOgnlContext oc = new OgnlContext();oc.setRoot(userRoot);oc.setValues(contextMap);//OGNL取值//給Roog中userRoot對象的name屬性賦值Ognl.getValue("name='grf'", oc, oc.getRoot());//賦值 有返回值,返回值是name的值String name = (String) Ognl.getValue("name='grf',name", oc, oc.getRoot());//即賦值又取值//給Context中user1的name屬性賦值Ognl.getValue("#user1.name='grf'", oc, oc.getRoot());}@Test//為屬性賦值(set get)public void fun3() throws OgnlException{//OGNL表達式//準備ROOTUser userRoot = new User("ms",25);//準備ContextMap<String,User> contextMap = new HashMap<String, User>();contextMap.put("user1", new User("AAA",10));contextMap.put("user2", new User("BBB",11));//書寫OGNLOgnlContext oc = new OgnlContext();oc.setRoot(userRoot);oc.setValues(contextMap);//OGNL取值//給Roog中userRoot對象的name屬性賦值Ognl.getValue("setName('grf')", oc, oc.getRoot());//賦值 返回值為nullString name = (String) Ognl.getValue("getName()", oc, oc.getRoot());//即賦值又取值//給Context中user1的name屬性賦值Ognl.getValue("#user1.setName('grf'),#user1.getName()", oc, oc.getRoot());}@Test//調用靜態方法,或者靜態屬性public void fun4() throws OgnlException{//OGNL表達式//準備ROOTUser userRoot = new User("ms",25);//準備ContextMap<String,User> contextMap = new HashMap<String, User>();contextMap.put("user1", new User("AAA",10));contextMap.put("user2", new User("BBB",11));//書寫OGNLOgnlContext oc = new OgnlContext();oc.setRoot(userRoot);oc.setValues(contextMap);//OGNL取值//給Roog中userRoot對象的name屬性賦值Double pi = (Double) Ognl.getValue("@java.lang.Math@PI", oc, oc.getRoot());//賦值 返回值為null System.out.println(pi);}@Test//創建集合 list|mappublic void fun5() throws OgnlException{//OGNL表達式//準備ROOTUser userRoot = new User("ms",25);//準備ContextMap<String,User> contextMap = new HashMap<String, User>();contextMap.put("user1", new User("AAA",10));contextMap.put("user2", new User("BBB",11));//書寫OGNLOgnlContext oc = new OgnlContext();oc.setRoot(userRoot);oc.setValues(contextMap);//OGNL取值//創建listOgnl.getValue("{'aaa','bbb','ccc','ddd'}", oc, oc.getRoot());Integer listSize = (Integer) Ognl.getValue("{'aaa','bbb','ccc','ddd'}.size()", oc, oc.getRoot());String listName = (String) Ognl.getValue("{'aaa','bbb','ccc','ddd'}[0]", oc, oc.getRoot());String listName1 = (String) Ognl.getValue("{'aaa','bbb','ccc','ddd'}.get(1)", oc, oc.getRoot());//創建mapOgnl.getValue("#{'name':'ms','age',25}", oc, oc.getRoot());Integer mapSize = (Integer) Ognl.getValue("#{'name':'ms','age',25}.size()", oc, oc.getRoot());String mapName = (String) Ognl.getValue("#{'name':'ms','age',25}[name]", oc, oc.getRoot());Integer mapAge = (Integer) Ognl.getValue("#{'name':'ms','age',25}.get('age')", oc, oc.getRoot());} }
?