ognl表达式语法(取值,赋值,调用方法,调用静态方法,创建对象List,Map)

本文介绍如何利用Ognl表达式在Java中进行对象的取值、赋值、方法调用及静态方法调用,同时展示了创建List和Map的操作。通过实例演示了Ognl的强大功能。

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

准备:创建一个User实体

public class User {
	private String name;
	private Integer age;
	public User(String name, Integer age) {
		super();
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	
}

 

1.取值:

        @Test
	public void func2() throws OgnlException{
		User tom = new User("tom",12);
		
		Map<String,User> hashMap = new HashMap<>();
		hashMap.put("user1", new User("jack",20));
		hashMap.put("user2", new User("john",21));
		
		OgnlContext ognlContext = new OgnlContext();
		ognlContext.setRoot(tom);
		ognlContext.setValues(hashMap);
		
		String name = (String) Ognl.getValue("name", ognlContext, ognlContext.getRoot());
		Integer age = (Integer) Ognl.getValue("age", ognlContext, ognlContext.getRoot());
		System.out.println("name:"+name);
		System.out.println("age:"+age);
	}
        @Test
	public void func3() throws OgnlException{
		User tom = new User("tom",12);
		
		Map<String,User> hashMap = new HashMap<>();
		hashMap.put("user1", new User("jack",20));
		hashMap.put("user2", new User("john",21));
		
		OgnlContext ognlContext = new OgnlContext();
		ognlContext.setRoot(tom);
		ognlContext.setValues(hashMap);
		
		String name = (String) Ognl.getValue("#user1.name", ognlContext, ognlContext.getRoot());
		Integer age = (Integer) Ognl.getValue("#user2.age", ognlContext, ognlContext.getRoot());
		System.out.println("name:"+name);
		System.out.println("age:"+age);
	}

 2.赋值:

        @Test
	public void func4() throws OgnlException{
		User tom = new User("tom",12);
		
		Map<String,User> hashMap = new HashMap<>();
		hashMap.put("user1", new User("jack",20));
		hashMap.put("user2", new User("john",21));
		
		OgnlContext ognlContext = new OgnlContext();
		ognlContext.setRoot(tom);
		ognlContext.setValues(hashMap);
		
		Ognl.getValue("name='jerry1'", ognlContext, ognlContext.getRoot());
		Ognl.getValue("#user1.name='jerry2'", ognlContext, ognlContext.getRoot());
		String name1 = (String) Ognl.getValue("name", ognlContext, ognlContext.getRoot());
		String name2 = (String) Ognl.getValue("#user1.name", ognlContext, ognlContext.getRoot());
		
		System.out.println(name1);
		System.out.println(name2);
		
	}

 

 3.调用方法:

        @Test
	public void func5() throws OgnlException{
		User tom = new User("tom",12);
		
		Map<String,User> hashMap = new HashMap<>();
		hashMap.put("user1", new User("jack",20));
		hashMap.put("user2", new User("john",21));
		
		OgnlContext ognlContext = new OgnlContext();
		ognlContext.setRoot(tom);
		ognlContext.setValues(hashMap);
		
		Ognl.getValue("setName('lili')", ognlContext, ognlContext.getRoot());
		Ognl.getValue("#user1.setName('lucy1')", ognlContext, ognlContext.getRoot());
		String name = (String)Ognl.getValue("#user1.getName()", ognlContext, ognlContext.getRoot());
		System.out.println(name);
		
		
	}

4.调用静态方法:

        @Test
	public void func6() throws OgnlException{
		User tom = new User("tom",12);
		
		Map<String,User> hashMap = new HashMap<>();
		hashMap.put("user1", new User("jack",20));
		hashMap.put("user2", new User("john",21));
		
		OgnlContext ognlContext = new OgnlContext();
		ognlContext.setRoot(tom);
		ognlContext.setValues(hashMap);
		

		double a = (double) Ognl.getValue("@java.lang.Math@PI", ognlContext,ognlContext.getRoot());
		String name = (String)Ognl.getValue("@com.it.utils.AutoUtils@echo('hello world!')", ognlContext, ognlContext.getRoot());
		
		System.out.println(a);
		System.out.println(name);
	}

5.创建List,Map:

        @Test
	public void func7() throws OgnlException{
		User tom = new User("tom",12);
		
		Map<String,User> hashMap = new HashMap<>();
		hashMap.put("user1", new User("jack",20));
		hashMap.put("user2", new User("john",21));
		
		OgnlContext ognlContext = new OgnlContext();
		ognlContext.setRoot(tom);
		ognlContext.setValues(hashMap);
		
		Integer size = (Integer)Ognl.getValue("{'list0','list1','list2','list3'}.size()", ognlContext, ognlContext.getRoot());
		String value1 = (String)Ognl.getValue("{'list0','list1','list2','list3'}[0]", ognlContext, ognlContext.getRoot());
		String value2 = (String)Ognl.getValue("{'list0','list1','list2','list3'}[1]", ognlContext, ognlContext.getRoot());
		String value3 = (String)Ognl.getValue("{'list0','list1','list2','list3'}.get(3)", ognlContext, ognlContext.getRoot());
		
		System.out.println(size);
		System.out.println(value1);
		System.out.println(value2);
		System.out.println(value3);
		
		Integer mapSize = (Integer)Ognl.getValue("#{'name':'tom','age':12}.size()", ognlContext, ognlContext.getRoot());
		String name = (String)Ognl.getValue("#{'name':'tom','age':12}['name']", ognlContext, ognlContext.getRoot());
		Integer age1 = (Integer) Ognl.getValue("#{'name':'tom','age':12}['age']", ognlContext, ognlContext.getRoot());
		Integer age2 = (Integer) Ognl.getValue("#{'name':'tom','age':12}.get('age')", ognlContext, ognlContext.getRoot());
		
		System.out.println(mapSize);
		System.out.println(name);
		System.out.println(age1);
		System.out.println(age2);
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值