- package javaseognl;
- import ognl.OgnlContext;
- import ognl.Ognl;
- import ognl.OgnlException;
- /**
- * Created by IntelliJ IDEA.
- * User: Administrator
- * Date: 2011-12-29
- * Time: 15:18:46
- * To change this template use File | Settings | File Templates.
- */
- /**
- * person bean
- */
- class Person {
- private String name;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
- /**
- * dog bean
- */
- class Dog {
- private String name;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
- /**
- * Ognl测试类
- */
- public class OgnlTest {
- public static void main(String[] args) throws OgnlException {
- /**
- * 创建Ognl上下文对象
- */
- OgnlContext context = new OgnlContext();
- Person person = new Person();
- Dog dog = new Dog();
- person.setName("zhangsan");
- dog.setName("wangcai");
- /**
- * 把创建的对象放入Ognl上下文对象中
- */
- context.put("person", person);
- context.put("dog", dog);
- /**
- * 设置根对象
- */
- context.setRoot(person);
- /**
- * 解析参数根对象中查找
- */
- Object object = Ognl.parseExpression("name");
- Object o = Ognl.getValue(object, context, context.getRoot());
- /**
- * 取出根对象的属性
- */
- System.out.println(o);
- /**
- *获取person对象中的属性
- */
- Object object1 = Ognl.parseExpression("#person.name");
- Object o1 = Ognl.getValue(object1, context, context.getRoot());
- System.out.println(o1);
- /**
- * 获取dog对象中的属性
- */
- Object object2 = Ognl.parseExpression("#dog.name");
- Object o2 = Ognl.getValue(object2, context, context.getRoot());
- System.out.println(o2);
- }
- }