Learning OGNL grammar

Keywords: Java Attribute

Original link: http://www.yiidian.com/struts2/struts2-ognl.html

1 OGNL introduction

The full name of OGNL is object graph navigation language, which is a powerful open source expression language. With this expression language, you can store any properties of Java objects, call methods of Java objects, and automatically implement necessary type conversion through some expression syntax. If the expression is regarded as a kind of string with semantics, OGNL will undoubtedly become a bridge between the semantic string and Java objects.

2 OGNL data structure

The OGNL expression operates on an object called OgnlContext. The object contains two properties:

  • Root: root object, any object can be stored as root object
  • Context: context object. This is a Map structure. Any key value pair can be stored in the Map

We can access data through the OGNL expression for the above two parts of the properties of the OgnlContext object.

3 OGNL expression syntax

Basic rules of Ognl expression:

  • Take the value of the root object, just go through the root object attribute directly

  • The value of the context object must be obtained through the ා - key. Attribute in the specified context container

3.1 environmental preparation

First import the ognl package to the project

Design a User class to store data

package com.yiidian.ognl;
/**
 * @author A little tutorial (Yidian. Com)
 */
public class User {
	private String name;
	private Integer 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;
	}
	public User() {
		super();
	}
	public User(String name, Integer age) {
		super();
		this.name = name;
		this.age = age;
	}
	
}

3.2 basic value

/**
 * Basic value
 */
@Test
// Take out the attribute value in root
public void test1() throws Exception {
	// Prepare ONGLContext
	// Preparing for Root
	User rootUser = new User("tom", 18);
	// Prepare Context
	Map<String, User> context = new HashMap<String, User>();
	context.put("user1", new User("jack", 18));
	context.put("user2", new User("rose", 22));
	OgnlContext oc = new OgnlContext();
	oc.setRoot(rootUser);
	oc.setValues(context);
	
	// ====Using the OGNL expression to get the properties====
	// Take out the name attribute of user object in root
	String name = (String) Ognl.getValue("name", oc, oc.getRoot());
	Integer age = (Integer) Ognl.getValue("age", oc, oc.getRoot());
	System.out.println(name);
	System.out.println(age);

	// ---------------------------------------------------
	// Take the name attribute of the user1 object as the key in the context
	String name1 = (String) Ognl.getValue("#user1.name", oc, oc.getRoot());
	String name2 = (String) Ognl.getValue("#user2.name", oc, oc.getRoot());
	Integer age1 = (Integer) Ognl.getValue("#user2.age", oc, oc.getRoot());
	System.out.println(name1);
	System.out.println(name2);
	System.out.println(age1);
}

The result is:

3.3 assignment

/**
 * assignment
 */
@Test
public void test2() throws Exception {
	// Prepare ONGLContext
	// Preparing for Root
	User rootUser = new User("tom", 18);
	// Prepare Context
	Map<String, User> context = new HashMap<String, User>();
	context.put("user1", new User("jack", 18));
	context.put("user2", new User("rose", 22));
	OgnlContext oc = new OgnlContext();
	oc.setRoot(rootUser);
	oc.setValues(context);
	
	// Writing OGNL expressions
	// Assign the name attribute of the user object in root
	Ognl.getValue("name='jerry'", oc, oc.getRoot());
	String name = (String) Ognl.getValue("name", oc, oc.getRoot());

	String name2 = (String) Ognl.getValue("#user1.name='Zhang San',#user1.name",oc, oc.getRoot());
	System.out.println(name);
	System.out.println(name2);
}

The result is:

3.4 call method

/**
 * Calling method
 */
@Test
public void test3() throws Exception  {
	// Prepare ONGLContext
	// Preparing for Root
	User rootUser = new User("tom", 18);
	// Prepare Context
	Map<String, User> context = new HashMap<String, User>();
	context.put("user1", new User("jack", 18));
	context.put("user2", new User("rose", 22));
	OgnlContext oc = new OgnlContext();
	oc.setRoot(rootUser);
	oc.setValues(context);
	
	// Writing OGNL expressions
	// Call the setName method of the user object in root
	Ognl.getValue("setName('lilei')", oc, oc.getRoot());
	String name = (String) Ognl.getValue("getName()", oc, oc.getRoot());

	String name2 = (String) Ognl.getValue("#user1.setName('lucy'),#user1.getName()", oc, oc.getRoot());

	System.out.println(name);
	System.out.println(name2);
}

The result is:

3.5 calling static methods

1) Design an OgnlUtil class with a static method of sayHello

package com.yiidian.ognl;
/**
 * @author A little tutorial (Yidian. Com)
 */
public class OgnlUtil {

	public static String sayHello(String name){
		return "Hello,"+name;
	}
}
//Call sayHello static method with Ognl expression

/**
 * Call static method
 */
@Test
public void test4() throws Exception {
	// Prepare ONGLContext
	// Preparing for Root
	User rootUser = new User("tom", 18);
	// Prepare Context
	Map<String, User> context = new HashMap<String, User>();
	context.put("user1", new User("jack", 18));
	context.put("user2", new User("rose", 22));
	OgnlContext oc = new OgnlContext();
	oc.setRoot(rootUser);
	oc.setValues(context);
	
	// Writing OGNL expressions
	String name = (String) Ognl.getValue("@com.yiidian.ognl.OgnlUtil@sayHello('Zhang San')", oc,oc.getRoot());
	Double pi = (Double) Ognl.getValue("@@PI", oc, oc.getRoot());
	System.out.println(name);
	System.out.println(pi);
}

3.6 create objects (List, Map)

/**
 * Create objects (List, Map)
 */
@Test
public void test5() throws Exception {
	// Prepare ONGLContext
	// Preparing for Root
	User rootUser = new User("tom", 18);
	// Prepare Context
	Map<String, User> context = new HashMap<String, User>();
	context.put("user1", new User("jack", 18));
	context.put("user2", new User("rose", 22));
	OgnlContext oc = new OgnlContext();
	oc.setRoot(rootUser);
	oc.setValues(context);
	
	// Writing OGNL expressions
	// Create list object
	Integer size = (Integer) Ognl.getValue("{'tom','jerry','jack','rose'}.size()", oc, oc.getRoot());
	String name = (String) Ognl.getValue("{'tom','jerry','jack','rose'}[0]", oc, oc.getRoot());
	String name2 = (String) Ognl.getValue("{'tom','jerry','jack','rose'}.get(1)", oc, oc.getRoot());
	
	System.out.println(size); 
	System.out.println(name);
	System.out.println(name2);
	 
	// Create Map object
	Integer size2 = (Integer) Ognl.getValue("#{'name':'tom','age':18}.size()", oc, oc.getRoot());
	String name3 = (String) Ognl.getValue("#{'name':'tom','age':18}['name']", oc, oc.getRoot());
	Integer age = (Integer) Ognl.getValue("#{'name':'tom','age':18}.get('age')", oc, oc.getRoot());
	System.out.println(size2);
	System.out.println(name3);
	System.out.println(age);
}

The result is:

Source code download: https://pan.baidu.com/s/1lD59FmfQLsG1h1Yvx55WUg

Welcome to my official account: a little tutorial. Get exclusive learning resources and daily dry goods push.
If you are interested in my series of tutorials, you can also follow my website: yiidian.com

Posted by toodi4 on Fri, 17 Apr 2020 17:57:11 -0700