Selecting From Collections

OGNL provides a simple way to use an expression to choose some elements from a collection and save the results in a new collection. We call this "selection," from the database term for choosing a subset of rows from a table. For example, this expression:

listeners.{? #this instanceof ActionListener}

returns a list of all those listeners that are instances of the ActionListener class. See the coercion section for how OGNL treats various kinds of objects as collections.

Selecting First Match

In order to get the first match from a list of matches, you could use indexing such as listeners.{? true }[0]. However, this is cumbersome because if the match does not return any results (or if the result list is empty) you will get an ArrayIndexOutOfBoundsException.

The selection syntax is also available to select only the first match and return it as a list. If the match does not succeed for any elements an empty list is the result.

objects.{^ #this instanceof String }

Will return the first element contained in objects that is an instance of the String class.

Selecting Last Match

Similar to getting the first match, sometimes you want to get the last element that matched.

objects.{$ #this instanceof String }

This will return the last element contained in objects that is an instanceof the String class