OGNL Part 1: Getting started

In this series, I am going to explain the OGNL Language Guide with code example. In this first part, let’ se how to setup the Maven project in Eclipse 3.6 and run the first example.

Here the Maven POM file

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>au.com.ojitha</groupId>
  <artifactId>OgnalExample-1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
  	<dependency>
	    <groupId>ognl</groupId>
	    <artifactId>ognl</artifactId>
	    <version>3.0.2</version>
	</dependency>
  </dependencies>
</project>

Here the simplest example.

package au.com.ojitha.ongl;

import ognl.Ognl;
import ognl.OgnlException;

public class FirstExample {

	/**
	 * @param args
	 * @throws OgnlException 
	 */
	public static void main(String[] args) throws OgnlException {
		Person p = new Person();
		p.setName("Ojitha");
		System.out.println(Ognl.getValue("name.toCharArray()[2]", p));

	}
	
}

class Person {
	private String name;

	public String getName() {
		return name;
	}

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

As shown in the above FirstExample class, the output is “i”.

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