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.
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.