Ognl 表达式

Ognl,对象视图导航语言,${user.name}这种写法就叫对象视图导航 相比EL 比EL功能更加强大
ognl 由  root和context组成  root 对数据格式没有要求,但context必须是map

package leitao.zhang.a_ognl;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import leitao.zhang.domain.User;
import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;

public class Demo1 {

	@Test
	public void fun1() throws OgnlException
	{
		
		User rootUser = new User("张三", 23);
		
		Map<String,User> ognlContext = new HashMap<String,User>();
		ognlContext.put("user1",new User("李四",24));
		ognlContext.put("user2",new User("王五",25));
		//OGNLcontext对象
		OgnlContext context = new OgnlContext();
		context.setRoot(rootUser);
		context.setValues(ognlContext);
		
		//取root里的值
		Object value = Ognl.getValue("name", context, rootUser); 
		System.out.println("value:"+value);
		//取OgnlContext中的值
		Object value2 = Ognl.getValue("#user1.name", context, rootUser);
		System.out.println("value2:"+value2);
		
		//修改Root中的值 ,可获取多个值 但只返回最后一个
		Object value3 = Ognl.getValue("name,age", context, rootUser);
		System.out.println("value3:"+value3);
		
		Object value4 = Ognl.getValue("name='张wu',age=26,name", context, rootUser);
		System.out.println("value4:"+value4);
		//修改值,如果多个表达式返回最后一个修改后的值
		Object value5 = Ognl.getValue("name='张wu',age=26", context, rootUser);
		System.out.println("value5:"+value5);
		
		//调用静态方法 @
		Object value6 = Ognl.getValue("@leitao.zhang.a_ognl.AbcTest@echo('HAHA')", context, rootUser);
		System.out.println("value6:"+value6);
		//默认类是Math
		Object value7 = Ognl.getValue("@@floor(10.2)", context, rootUser);
		System.out.println("value7:"+value7);
		//调用属性

		//这个不知道为啥不可以
		/*Object value7 = Ognl.getValue("@leitao.zhang.a_ognl.AbcTest@name", context, rootUser);
		System.out.println("value7:"+value7);*/
		Object value8 = Ognl.getValue("@@PI", context, rootUser);
		System.out.println("value8:"+value8);
		//创建list集合
		Object value9 = Ognl.getValue("{'zhansan','lisi','wangwu','zhaoliu'}[0]", context, rootUser);
		System.out.println("value9:"+value9);
		Object value10 = Ognl.getValue("{'zhansan','lisi','wangwu','zhaoliu'}.get(1)", context, rootUser);
		System.out.println("value10:"+value10);
		//创建map集合  需要以 # 开头
		Object value11 = Ognl.getValue("#{'name':'zhansan','age':23,'address':'河北'}['name']", context, rootUser);
		System.out.println("value11:"+value11);
		Object value12 = Ognl.getValue("#{'name':'zhansan','age':23,'address':'河北'}.get('address')", context, rootUser);
		System.out.println("value12:"+value12);
		//map嵌套list
		Object value13 = Ognl.getValue("#{'zhansan':{'xiaoming','xiaoli'}}.get('zhansan').get(1)", context, rootUser);
		System.out.println("value13:"+value13);
		//list嵌套map 这种不行
		/*Object value14 = Ognl.getValue("{{'name':'zhansan','age':23,'address':'河北'},{'name':'lisi','age':24,'address':'河西'}}.get(1).get('name')", context, rootUser);
		System.out.println("value13:"+value14);*/
	}
}

class AbcTest{
	public static String name="王武是个卖烧饼的";
	public static Object echo (Object o)
	{
		return o.toString()+"--oooo";
	}
}

未经允许不得转载:开心乐窝-乐在其中 » Ognl 表达式

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏