Since iteration is a built-in function of OGNL and many objects support the idea of iterating over the contents of an object (i.e. the object.{ ... }
syntax) OGNL provides a hook into how iteration is done. The
ElementsAccessor
interface defines how iteration is done based on a source object. Simple examples could be a Collection
elements accessor, which would simply
public interface ElementsAccessor { public Enumeration getElements( Object target ) throws OgnlException; }
You can set a method accessor on a class-by-class basis using OgnlRuntime.setElementsAccessor()
. There are default elements accessors for Object
(which returns an Enumeration
of itself as the only object), Map
(which iterates over the values in the Map
), and Collection (which uses the collection's iterator()
). One clever use of
ElementsAccessor
s is the NumberElementsAccessor
class which allows for generating numeric sequences from 0 to the target value. For example the expression (100).{ #this }
will
generate a list of 100 integers ranged 0..99.