OGNL Part 2: Navigation Chain

In the part 1 of this series explained how to setup the Eclipse project for testing. The navigation chain is the fundamental thing in the OGNL. See the following example.

package au.com.ojitha.ongl;

import ognl.Ognl;
import ognl.OgnlException;

public class FirstEcample {

	/**
	 * @param args
	 * @throws OgnlException 
	 */
	public static void main(String[] args) throws OgnlException {
		Person p = new Person();
		p.setName(new Name());
		
		//set the value
		Ognl.setValue("name.firstName", p, "Ojitha");
		Ognl.setValue("name.lastName", p, "Kumanayaka");
		
		//retrieve the value
		System.out.println(Ognl.getValue("name.firstName+' '+name.lastName", p));

	}
	
}

class Person {
	private Name name;

	public Name getName() {
		return name;
	}

	public void setName(Name name) {
		this.name = name;
	}
	
}

class Name {
	
	private String firstName;
	private String lastName;
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
}

As shown in the above example code, line 17 and 18 shows how to set the value in the navigation path and line number 21 show how to retrieve the values using navigation path access. The output of this is “Ojitha Kumanaya”.

Comments

Popular posts from this blog

How To: GitHub projects in Spring Tool Suite

Spring 3 Part 7: Spring with Databases

Parse the namespace based XML using Python