OGNL Part 7: Tutorial on OGNL Selection

We’ve already discussed the  Selection in  the part 6. Here the example of selecting first element or last element in the filtered array or list.

  • ^ for the first element (lines 19-20)
  • $ for the last element (lines 23-24)
package au.com.ojitha.ongl;

import ognl.Ognl;
import ognl.OgnlException;

public class SelectionTutorial {

	/**
	 * @param args
	 * @throws OgnlException
	 */
	public static void main(String[] args) throws OgnlException {
		Root root = new Root();
		// selection
		System.out.println("slect integer which are greater than 2: "
				+ Ognl.getValue("array.{? #this > 2}", root));
		
		// return the first element
		System.out.println("select the first element in the array: "
				+ Ognl.getValue("array.{^ #this > 2}", root));
		
		//return the last element
		System.out.println("select the last element in the array: "
				+ Ognl.getValue("array.{$ #this > 2}", root));

	}

}

class Root {
	private int[] array = { 1, 2, 3, 4, 5, 6 };

	public int[] getArray() {
		return array;
	}

	public void setArray(int[] array) {
		this.array = array;

	}

}

Here the output

slect integer which are greater than 2: [3, 4, 5, 6]
select the first element in the array: [3]
select the last element in the array: [6]

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