JavaWeb framework _ Struts2 _ (4) -----> Expression Language OGNL, javaweb_struts2 _
1. Expression Language OGNL
- Introduction to OGNL
- Basic OGNL syntax
- Constant
- Operator
- OGNL expression
- OGNL Basics
- OGNL Context
- OGNL value Stack
- OGNL access
2. Details
2.1 Introduction to OGNL
OGNL (Object-Graph Navigation Language) refers to the image Navigation Language of an Object. OGNL is an Expression Language (EL ). You can access any property of an object, call the object method, traverse the structure of the entire object, and convert the field type.
OGNL is a language that represents and binds the getter and setter attributes of java objects. Generally, OGNL uses simple and consistent expression syntax to access the attributes of an object.
2.2 Basic OGNL syntax
2.2.1 Constants
- String constant: A string enclosed by single or double quotation marks. Supports escape characters "\";
- Character constant: A character enclosed in single quotes, for example, 'C ';
- Numerical constants: int, long, float, and double in java;
- Boolean constants: true and false;
- Null constant.
The sample code for outputting Constants on the JSP page is as follows:
<% @ Taglib prefix = "s" uri = "/struts-tags" %> <! -- Add the tag library of Struts2 --> <s: property value = "'you said, \" Hello World \". '"/> <br> <s: property value =" true "/> <br> <s: property value =" null "/> <br> <s: property value = "12.3f"/>
2.2.2 Operator
The OGNL comma supports all Java operators (+,-, *,/, ++, --, And ,--,! =, Etc.) similar to java, it also provides some special operation symbols. The following describes some special operation symbols of OGNL.
1. Comma (,) or sequence Operator
A comma is used to separate two or more independent expressions. The value of the entire expression is the value of the last expression. For example:
Team1.person. name, team2.teamname;
The value of the entire expression is the value of the second expression.
2. Braces operator {};
The braces operator is used to create a table (array ). Enclose elements with braces and separate them with commas. For example:
{"Mayun", "liyanhong", "mahuateng"} [1];
This expression creates a list with three elements and accesses the second element. The code for outputting the second element value on the JSP page is as follows:
<s:property value="{'mayun','liyanhong','mahuateng'}[1]"/>
3. Operators of in and not in
In and not in are used to determine whether a value belongs to a set. For example:
Teamname in {'team1', 'team2 '};
This expression determines whether teamname is in the array {'team1', 'team2'}. If true is returned, false is not returned.
2.2.3 OGNL expression
You can use OGNL expressions to access attributes, methods, static attributes and methods, constructor methods, arrays, and collections.
1. Access attributes
The OGNL access attribute sample code is as follows:
<! -- Access property --> <s: property value = "uname"/>
<! -- Access Object Attributes (get and set) --> <s: property value = "user. age"/> <s: property value = "employee. manager. uname"/>
2. Access Method
The OGNL access method sample code is as follows:
<! -- Access Object Method --> <s: property value = "employee. eat ()"/> <! -- Method for accessing action --> <s: property value = "add ()"/>
3. Access static attributes and Methods
OGNL supports calling static methods and static fields in the class. You can use the following syntax format: @ class @ method (args); @ class @ field; where, the class must provide the complete class name, for example, @ java. lang. string @ valueOf (5 ). If the class is omitted, java. lang. Math is used by default. The sample code is as follows:
<! -- Access static method --> <s: property value = "@ com. my. ognl. StaticSample @ ma ()"/> <! -- Access static properties --> <s: property value = "@ com. my. ognl. StaticSample @ STR"/> <! -- Static Method for accessing Math --> <s: property value = "@ max ()"/>
Note that in the configuration file (Struts. in xml), add the following constant (clipped in the <struts> tag and above the <package> tag) to OGNL to access static attributes and methods:
<constant name = "Struts.ognl.allowStaticMethodAccess" value = "true"/>
As shown in:
4. Access Constructor
The OGNL access constructor sample code is as follows:
<! -- Access constructor --> <s: property value = "new com.my.org. User (44)"/>
5. Access set and Value
You can use the OGNL language to directly generate collection elements (such as List objects or Map objects). You can use the following code to directly generate a List object:
{E1, e2, e3 };
You can use the following code to directly generate a Map object:
# {key1:value1 , key2:value2 , key3:value3};
Map Collection objects are defined in the key-value format. Each key-value element is identified by a colon, and multiple elements are separated by commas. For the collection type OGNL, you can use the in and not in element symbols to determine whether the element is in the specified set.
Note that OGNL can reference some special attributes of a set. These attributes are not in the JavaBeans mode, such as size. When an expression references these attributes, OGNL calls the corresponding method, which is a pseudo attribute.
The special set of pseudo-books in OGNL are shown in the following table:
| Set Type |
Pseudo attribute |
OGNL expression |
Java code |
| List, Set, Map |
Size; isEmpty |
List. size; set. isEmpty |
List. size (); set. isEmpty |
| List, Set |
Iterator |
List. iterator; set. iterator |
List. iterator (); set. iterator () |
| Map |
Keys, values |
Map. keys; map. values |
Map. keySet (); map. values () |
| Iterator |
Next, hasNext |
It. next; it. hasNext |
It. next (); it. hasNext () |
6. Projection
OGNL provides a simple way to call the same method for each element in a set, or extract the same attributes, and save the results as a new set called projection.
Suppose that employees is a list containing the employee object, then:
# Employees. {name} // returns the list of names of all employees.
During projection, use# This variable to reference the current element in the iteration. For example:
Object.{#this instanceof String? #this: #this.toString()}
7. Select
OGNL provides a simple way to use expressions to select certain elements from the set and save the results to the new set, which is called selection. The special characters in the selection expression are described as follows:
- ? : Select the matching LogicAllElement;
- ^: Select the logicalFirstElement;
- $: Select a matchedLastElement;
The sample code is as follows:
<! -- The list of employees whose salaries exceed 3000 will be returned --> # employees .{? # This. salary & gt; 3000}
<! -- The first employee with a salary of more than 3000 will be returned --> # employees. {^ # this. salary> 3000}
<! -- The last employee with a salary greater than 3000 will be returned --> # employees. {$ # this. salary> 3000}
8. OGNL expression symbol
The # And % symbols in Struts2 OGNL are described as follows:
(1) # symbol
- Access non-root object attributes, such as # session. msg expression, # equivalent to ActionContext. getContext ();
- Used to select and project sets, such as person .{? # This. age> 30 };
- Used to construct a Map set, such as # {0: male, 1: Female}
(2) % symbol
The purpose of the % symbol isWhen the flag property is of the string type, the OGNL expression value is calculated.. For example, the following code is as follows:
<s:set name = "foobar" value="# {'foo1':'bar','foo2':'bar2'}"/> <s:url value="%{#foobar['fool']}"/>
2.3 OGNL Basics
2.3.1 OGNL Context
OGNL computing is based on context. OGNL context is actually a Map object, represented by the ognl. OgnlContext class (which implements the java. util. Map interface.
Struts2 sets OGNLContext to ActionContext, that is, the implementation of OGNL Context (Context) in Struts2 is ActionContext. Struts2 uses ValueStack (value stack) as the root object of OGNL.
When Struts2 accepts a request, it will quickly create ActionContext, ValueStack, and action. Then, the action is stored in ValueStack, so the instance variable of the action can be accessed by OGNL.In Struts2, The OGNL expression must be used with the Struts2 tag, as shown below:
<s:property value = "uname"/>
2.3.2 value Stack
The value stack is the root object (root object) of OGNL. Similar to the normal Stack, it conforms to the "first-in-first-out" feature and can be placed, deleted, and queried in the value stack, value stack is the core of Struts2.
If you want to access the attributes of an object in the root object (that is, ValueStack), you can omit # Namespace. You can directly access the attributes of this object.Read the data in the stack as follows.
<! -- Read the elements in the stack in the following way: --> <s: property value = "# root [0]. loginname "/> <s: property value =" # root [1]. loginname "/> <s: property value =" # root [2]. loginname "/>
2.3.3 OGNL access
ValueStack is the root object of OGNL in Struts2. If you need to access the objects in the value stack, you can access the attributes of objects in ValueStack by entering the following expression language (EL) on the JSP page:
<! -- Get the foo attribute of an object in the value Stack -- >$ {foo} <! -- You can also access objects in ValueStack through Struts tags --> <s: property value = "foo"/>
If you access objects in other contexts, because they are not the root objects, you must add the # prefix during access. The details are as follows: