欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

OGNL表达式的实战应用:取值、赋值、方法调用(包括实例与静态)、创建List和Map对象详解

最编程 2024-02-09 20:21:12
...

准备:创建一个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);
	}