码迷,mamicode.com
首页 > 其他好文 > 详细

Ognl的底层用法

时间:2014-10-27 09:21:07      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:ognl   java   

今天学了下ognl的运用,在这里和大家分享一下。希望对大家有所帮助。


package com.hcj.test;

import java.util.ArrayList;
import java.util.List;

import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;

public class Test {
	public static void main(String[] args) throws OgnlException {
		Person p = new Person();
		p.setName("张三");
		
		Cat cat = new Cat();
		cat.setName("花猫");
		
		//创建ognl上下文
		OgnlContext ognlContext = new OgnlContext();
		ognlContext.put("person", p);
		ognlContext.put("cat", cat);
		//设置person根对象
		ognlContext.setRoot(p);
		//获取根对象
		Object root = ognlContext.getRoot();
		
		//获取根对象的属性
		Object obj1 = Ognl.parseExpression("name");
		System.out.println(obj1);
		Object value1 = Ognl.getValue(obj1, ognlContext, root);
		System.out.println(value1);
		System.out.println("==========");
		
		//访问非根对象的属性
		Object obj2 = Ognl.parseExpression("#cat.name");
		System.out.println(obj2);
		Object value2 = Ognl.getValue(obj2, ognlContext, root);
		System.out.println(value2);
		System.out.println("==========");
		
		//访问类的静态方法
		Object obj3 = Ognl.parseExpression("@java.lang.Math@PI");
		System.out.println(obj3);
		Object value3 = Ognl.getValue(obj3, ognlContext, root);
		System.out.println(value3);
		System.out.println("======");
		
		//访问math方法
		Object obj4 = Ognl.parseExpression("@@max(1,3)");
		System.out.println(obj4);
		Object value4 = Ognl.getValue(obj4, ognlContext, root);
		System.out.println(value4);
		System.out.println("======");
		
		//创建对象
		Object obj5 = Ognl.parseExpression("new com.hcj.test.Cat()");
		System.out.println(obj5);
		Object value5 = Ognl.getValue(obj5, ognlContext, root);
		System.out.println(value5);
		System.out.println("======");
		
		//创建list对象
		Object obj6 = Ognl.parseExpression("{'a','b','c'}");
		System.out.println(obj6);
		Object value6 = Ognl.getValue(obj6, ognlContext, root);
		System.out.println(value6);
		System.out.println("======");
		
		//访问list对象属性
		Object obj7 = Ognl.parseExpression("{'a','b','c'}[1]");
		System.out.println(obj7);
		Object value7 = Ognl.getValue(obj7, ognlContext, root);
		System.out.println(value7);
		System.out.println("======");
		
		//创建map对象
		Object obj8 = Ognl.parseExpression("#{'1':'aa', '2':'bb', '3':'cc'}");
		System.out.println(obj8);
		Object value8 = Ognl.getValue(obj8, ognlContext, root);
		System.out.println(value8);
		System.out.println("======");
		
		//访问map对象属性
		Object obj9 = Ognl.parseExpression("#{'1':'aa', '2':'bb', '3':'cc'}['3']");
		System.out.println(obj9);
		Object value9 = Ognl.getValue(obj9, ognlContext, root);
		System.out.println(value9);
		System.out.println("======");
		
		//选择,格式: collection.{? expression}
		List<Person> persons = new ArrayList<Person>();
		Person p1 = new Person();
		p1.setName("zz12345");
		Person p2 = new Person();
		p2.setName("aa121312");
		Person p3 = new Person();
		p3.setName("bb");
		
		persons.add(p1);
		persons.add(p2);
		persons.add(p3);
		//将persons放入ognl上下文中
		ognlContext.put("persons", persons);
		
		Object obj10 = Ognl.parseExpression("#persons.{? #this.name.length() > 2}");
		System.out.println(obj10);
		Object value10 = Ognl.getValue(obj10, ognlContext, root);
		System.out.println(value10);
		System.out.println("======");
		
		//投影,格式: collection.{? expression}
		Object obj11 = Ognl.parseExpression("#persons.{#this.name}");
		System.out.println(obj11);
		Object value11 = Ognl.getValue(obj11, ognlContext, root);
		System.out.println(value11);
		System.out.println("======");
		
	}
}

输出结果:

name
张三
==========
#cat.name
花猫
==========
@java.lang.Math@PI
3.141592653589793
======
@java.lang.Math@max(1, 3)
3
======
new com.hcj.test.Cat()
com.hcj.test.Cat@1dff3a2
======
{ 'a', 'b', 'c' }
[a, b, c]
======
{ 'a', 'b', 'c' }[1]
b
======
#{ '1' : "aa", '2' : "bb", '3' : "cc" }
{1=aa, 2=bb, 3=cc}
======
#{ '1' : "aa", '2' : "bb", '3' : "cc" }['3']
cc
======
#persons.{? (#this.name.length() > 2) }
[com.hcj.test.Person@b988a6, com.hcj.test.Person@ba6c83]
======
#persons.{ #this.name }
[zz12345, aa121312, bb]
======


Ognl的底层用法

标签:ognl   java   

原文地址:http://blog.csdn.net/u011768325/article/details/40502671

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!