Skip to content

Property Navigation

tporcham edited this page Jan 30, 2019 · 1 revision

MVEL property navigation follows well-established conventions found in other bean property expressions found in other languages such as Groovy, OGNL, EL, etc.

Unlike some other languages which require qualification depending on the underlying method of access, MVEL provides a single, unified syntax for accessing properties, static fields, maps, etc.

Bean Properties

Most java developers are familiar with and user the getter/setter paradigm in their Java objects in order to encapsulate property accessors. For example, you might access a property from an object as such:
user.getManager().getName();

In order to simplify this, you can access the same property using the following expression:
user.manager.name

Note: In situations where the field in the object is public, MVEL will still prefer to access the property via it's getter method.

Collections

Traversal of collections can also be achieved using abbreviated syntax.

List Access

Lists are accessed the same as array's. For example:
user[5]
is the equivalent of the Java code:
user.get(5);

Map Access

Maps are accessed in the same way as array's except any object can be passed as the index value. For example:
user["foobar"]
is the equivalent of the Java code:
user.get("foobar");

For Maps that use a String as a key, you may use another special syntax:
user.foobar
... allowing you to treat the Map itself as a virtual object.

Strings as Arrays

For the purposes of using property indexes (as well as iteration) all Strings are treated as arrays. In MVEL you may refer to the first character in a String variable as such:
foo = "My String"; foo[0]; // returns 'M'

See Also