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表达式 //准备ROOT User userRoot = new User("ms",25); //准备Context MapcontextMap = new HashMap (); contextMap.put("user1", new User("AAA",10)); contextMap.put("user2", new User("BBB",11)); //书写OGNL OgnlContext 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表达式 //准备ROOT User userRoot = new User("ms",25); //准备Context Map contextMap = new HashMap (); contextMap.put("user1", new User("AAA",10)); contextMap.put("user2", new User("BBB",11)); //书写OGNL OgnlContext 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表达式 //准备ROOT User userRoot = new User("ms",25); //准备Context Map contextMap = new HashMap (); contextMap.put("user1", new User("AAA",10)); contextMap.put("user2", new User("BBB",11)); //书写OGNL OgnlContext 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表达式 //准备ROOT User userRoot = new User("ms",25); //准备Context Map contextMap = new HashMap (); contextMap.put("user1", new User("AAA",10)); contextMap.put("user2", new User("BBB",11)); //书写OGNL OgnlContext oc = new OgnlContext(); oc.setRoot(userRoot); oc.setValues(contextMap); //OGNL取值 //给Roog中userRoot对象的name属性赋值 Ognl.getValue("setName('grf')", oc, oc.getRoot());//赋值 返回值为null String 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表达式 //准备ROOT User userRoot = new User("ms",25); //准备Context Map contextMap = new HashMap (); contextMap.put("user1", new User("AAA",10)); contextMap.put("user2", new User("BBB",11)); //书写OGNL OgnlContext 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|map public void fun5() throws OgnlException{ //OGNL表达式 //准备ROOT User userRoot = new User("ms",25); //准备Context Map contextMap = new HashMap (); contextMap.put("user1", new User("AAA",10)); contextMap.put("user2", new User("BBB",11)); //书写OGNL OgnlContext oc = new OgnlContext(); oc.setRoot(userRoot); oc.setValues(contextMap); //OGNL取值 //创建list Ognl.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()); //创建map Ognl.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()); }}