The full name of OGNL is Object graph Navigation Language (object graph navigation language), which is a powerful expression language that allows you to read and set the property values of Java objects through simple and consistent expression syntax, invoke the object's methods, traverse the entire object's structure diagram, Implements functions such as field type conversions.
Why use OGNL
OGNL is more powerful than other expression languages, providing many advanced and required features such as powerful type conversion capabilities, execution of static or instance methods, cross-collection projection, and dynamic lambda expression definitions.
OGNL Foundation
The OGNL expression is computed around the OGNL context, and the OGNL context is actually a map object, by OGNL. The Ognlcontext class (implements the Java.util.Map interface) to represent. The OGNL context can contain one or more JavaBean objects, one of which is special, which is the root (root) object of the context. If you do not specify which object in the context is used when writing an expression, the root object is assumed to be the object on which the expression is based.
In the OGNL context, there can only be one root object, if you access the root object, then write the expression, directly write the object's properties, otherwise, you need to use the "#key" prefix, such as expression #namager.name.
OGNL-expression
The base unit of the OGNL expression is the navigation chain, usually referred to as the chain (chain). The simplest chain consists of the following parts:
1, attribute name: such as name and Manager.name;
2, method call: such as Manager.hashcode (), return the Manager object hash code;
3. Array index: such as emals[0], returns the first e-mail address in the message list of the current object.
All OGNL expressions are evaluated in the context of the current object, and a chain simply uses the result of the previous link in the chain as the current object evaluated in the next step. Let's look at the chain shown below:
Name.tochararray () [0].numericvalue.tostring ()
This expression is evaluated in the following steps:
1. Gets the Name property of the root object;
2. Call the ToCharArray () method on the string result;
3. Extracts the first character from a char array;
4, from the extracted character object upstream to the Numericvalue attribute (this character is represented as character object, character class has a Getnumericvalue () method);
5. Call the ToString () method on the result of the integer object.
The end result of this expression is the string returned by the ToString () method call that was last returned.
Constant
All constant types supported by OGNL:
1. String constants:
A string enclosed in single or double quotation marks. such as "Hello", ' hello '.
Note, however, that if you are a string constant of a single character, you must use double quotation marks.
2, character constant capacity:
A character enclosed in single quotation marks. such as ' a '.
3. Numeric constants:
In addition to int, long, float, and double in Java, OGNL also lets you specify the BigDecimal constant using the "B" or "B" suffix, specifying the BigInteger constant with the "H" "H" suffix.
4. Boolean Constants:
True and False.
5, NULL constant.
Operator
In addition to supporting all Java operators, OGNL supports the following kinds of things:
1, comma,
is similar to the comma operator in the C language.
2. Curly braces {}
Used to create a list of elements separated by commas.
3, in and not in
Used to determine whether a value is in the collection.
Accessing the properties of JavaBean
If there is an employee object as the root object of the OGNL context, for the following expression:
1. Name
The corresponding Java code is employee.getname ();
2, Address.country
The corresponding Java code is employee.getaddress (). Getcountry ();
Accessing static methods and static fields
@[email protected] (args)//Call static method
@[email protected]//Call static field
Where class must give the complete class name (including the package name), and if class is omitted, the default class used is Java.util.Math, such as:
@ @min (5,3)
@ @max (5,3)
@ @PI
Indexed access
The OGNL supports multiple indexing methods of access.
1. Arrays and List indexes
In Ognl, arrays and lists can be broadly considered to be the same.
such as: Array[0], list[0]. Expression: {' Zhangsan ', ' Lisi ', ' Wangwu '}[1] and so on.
2. Indexed properties of JavaBean
To use indexed properties, you need to provide two pair setter and Getter methods, one pair for arrays, and one pair for the elements in the array.
For example: There is an indexed attribute interest, and its getter and setter are as follows
Public string[] interest;
Public string[] Getinterest () {return interest;}
public void Setinterest (string[] interest) {this.interest=interest;}
Public String getinterest (int i) {return interest[i]}
public void setinterest (int i, String newinterest) {interest[i]=newinterest;}
For the expression Interest[2],ognl, the expression can be interpreted correctly, and the Getinterest (2) method is called. If it is set, the Setinterest (2,value) method is called.
3. Indexed properties of OGNL objects
The indexed properties of JavaBean can only be indexed using integral types, OGNL extends the concept of indexed properties and can be indexed using any object.
To manipulate a collection
1. Create a collection:
Create a list
Use curly braces to enclose elements, separating elements with commas. such as {' Zhangsan ', ' Lisi ', ' Wangwu '}
Create an array
Creating an array in OGNL is similar to creating an array in the Java language.
Create a map
Map uses special syntax to create #{"key": Value, ...}
If you want to specify the type of map you create, you can specify the class name of the map implementation class before the left curly brace. Such as:
#@[email protected]{"key": "Value",....}
The map is accessed via key, such as map["key" or Map.key.
2. Projection
OGNL provides a simple way to invoke the same method on each element in a collection, or to extract the same attributes and save the result as a new set, called a projection.
If employees is a list that contains the Employee object, then
#employees. {name} returns a list of all employees ' names.
During projection, use the #this variable to refer to the current element in the iteration.
such as: objects. {#this instanceof String? #this: #this. toString ()}
3. Select
OGNL provides an easy way to use expressions to select certain elements from a collection and save the results to a new collection, called a selection.
such as #employees. {? #this. salary>3000}
A list of all employees with a salary greater than 3000 will be returned.
#employees. {^ #this. salary>3000}
A list of employees with the first salary greater than 3000 will be returned.
#employees. {$ #this. salary>3000}
Returns a list of employees with the last salary greater than 3000.
Lambda expression
The syntax for a lambda expression is:: [...]. A lambda expression in OGNL can use only one parameter, which is referenced by #this.
Such as:
#fact =: [#this <=1? 1: #this * #fact (#this-1)], #fact (30)
#fib =: [#this ==0? 0: #this ==1? 1: #fib (#this-2) + #fib (#this-1)], #fib (11
Ongl expression of expression language