Struts的OGNL

本文详细介绍了OGNL(对象图导航语言)的基础语法及其在Java中的应用,包括访问对象属性、数组、List和Map,执行基本运算,调用方法和静态方法,创建List和Map对象,以及在复杂对象结构中的数据检索。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ognl.Ognl;
import ognl.OgnlException;
import ognl.OgnlRuntime;

import org.junit.Test;

import com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor;
import com.opensymphony.xwork2.util.CompoundRoot;

public class TestOgnl {
	@Test
	public void f(){
		System.out.println(Integer.parseInt("F"));
	}
	//OGNL的基础语法
	@Test
	public void test1() throws OgnlException{
		Foo foo = new Foo();
		foo.setId(100);
		foo.setName("java");
		foo.setAry(new String[]{"one","two","three"});
		foo.setList(Arrays.asList("A","B","C"));
		HashMap<String,String> map = 
			new HashMap<String,String>();
		map.put("one", "java");
		map.put("two", "javajava");
		map.put("three", "javajavajava");
		foo.setMap(map);
		//ognl引擎访问对象
		//格式: Ognl.getValue("OGNL表达式",root对象)
		//root对象是Ognl要操作的对象
		
		//1、基本类型属性
		System.out.println("####1、基本类型属性");
		System.out.println(Ognl.getValue("id", foo));
		System.out.println(Ognl.getValue("name", foo));
		
		//2、数组、List属性
		System.out.println("####2、数组、List属性");
		System.out.println(Ognl.getValue("ary[0]", foo));
		System.out.println(Ognl.getValue("list[1]", foo));
		
		//3、map属性
		System.out.println("####3、map属性");
		System.out.println(Ognl.getValue("map.one", foo));
		System.out.println(Ognl.getValue("map['two']", foo));
		
		//4、基本运算
		System.out.println("####4、基本运算");
		System.out.println(Ognl.getValue("id+100", foo));
		System.out.println(Ognl.getValue("\"What is\" +name", foo));
		System.out.println(Ognl.getValue("id>150", foo));
		
		//5、调用方法
		System.out.println("####5、调用方法");
		System.out.println(Ognl.getValue("name.toUpperCase()", foo));
		System.out.println(Ognl.getValue("list.size()", foo));
	
		//注意:方法的参数也可以使用属性
		System.out.println(Ognl.getValue(
				"map['three'].lastIndexOf(name)", foo));
		
		//6、调用静态方法,取出的属性为参数
		System.out.println("####6、静态方法");
		System.out.println(
				Ognl.getValue(
					"@java.util.Arrays@toString(ary)", foo));
		
		//7、ognl中只能创建List和Map对象
		System.out.println("####7、创建List对象");
		Object obj = Ognl.getValue("{1,2,3,4,5}", null);
		System.out.println(obj.getClass().getName());
		System.out.println(obj);
		
		System.out.println("####8、创建map对象");
		obj = Ognl.getValue(
				"#{1:'java',2:'javajava',3:'javajavajava'}", null);
		System.out.println(obj.getClass().getName());
		System.out.println(obj);
	}

	
	@Test
	public void test2() throws OgnlException{
		Dept dept = new Dept();
		dept.setName("dept1");
		List<Emp> list = new ArrayList<Emp>();
		list.add(new Emp(100,"emp1",10000,new Date()));
		list.add(new Emp(200,"emp2",15000,new Date()));
		list.add(new Emp(300,"emp3",20000,new Date()));
		list.add(new Emp(400,"emp4",25000,new Date()));
		dept.setEmpList(list);
		//演示1:
		String name = (String)Ognl.getValue("empList[0].name", dept);
		Double salary = (Double)Ognl.getValue("empList[0].salary", dept);
		System.out.println(name+":"+salary);
		
		//演示2:
		Object obj = Ognl.getValue("empList.{salary}", dept);		
	    System.out.println("演示2");
	    System.out.println(obj.getClass().getName());
	    System.out.println(obj);
	    
	    //演示3:
	    //过滤演示:找出薪水大于15000的员工姓名
	    obj = 
	    	Ognl.getValue(
	    			"empList.{?#this.salary>=15000}.{name}", 
	    			dept);
	    System.out.println(obj.getClass().getName());
	    System.out.println(obj);
	}
	
	@Test
	public void test3()throws OgnlException{
		//自定义context对象,如果不写,系统会自动添加
		Map ctx = new HashMap();
		ctx.put("num", 10);
		
		//root对象
		Bar root = new Bar();
		root.setName("bar");
		
		//不加"#",表示从业务对象root当中获取
		System.out.println(Ognl.getValue("name",ctx, root));
		//加"#",表示从公共对象context中获取数据
		System.out.println(Ognl.getValue("#num",ctx, root));
	}
	
	@Test
	public void test4()throws OgnlException{
		//创建一个CompoundRoot对象
		CompoundRoot root = new CompoundRoot();
		Bar bar1 = new Bar();
		bar1.setName("hahaha bar1");
		root.push(bar1);
		
		Bar bar2 = new Bar();
		bar2.setName("hehehe bar2");
		root.push(bar2);
//		root.add(bar2)
		
		//定制Ognl的root机制为CompoundRoot机制
		OgnlRuntime.setPropertyAccessor(CompoundRoot.class, 
				new CompoundRootAccessor());
		String name = (String)Ognl.getValue("name", root);
		System.out.println(name);
	}
		
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值