The OGNL expression is an Object-Graph Navigation Language. OGNL is an open-source project. By default, the OGNL expression language is used in struts2 to display data. This is the same as the el expression in serlvet. OGNL expressions have the following features:
Supports object method calls, for example, objName. methodName (); supports static class method calls and value access. The expression format is @ [full class name (including package path)] @ [method name | value name] example: @ java. lang. string @ format ('foo % s', 'bar') @ tutorial. myConstant @ APP_NAME; Supports the assignment operation and expression concatenation, for example, price = 100, discount = 0.8, calculatePrice (), this expression returns 80; Access OGNL context (OGNL context) and ActionContext operation set object class diagram
ValueStack
ValueStack is an interface. In struts2, OGNL expressions are actually used to implement the ValueStack Interface Class OgnlValueStack, which is the basis of OgnlValueStack. ValueStack runs throughout the lifecycle of an action. Each action instance has a ValueStack object. The current action object and other related objects are saved. Struts2 saves the ValueStack object in the request domain named struts. valueStack.
ValueStack memory Diagram
Organization structure of ValueStack
From the figure, we can see that OgnlValueStack and our related content have two parts: OgnlContext and CompoundRoot. Therefore, it is important to clarify the two parts.
Overview
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + release + 7XEyv2 + release + 0 s/bK/release + zb/J0tTN6rPJttTK/release/09DBvbj2wt + 8rbXE1 + mzybK/release/X97rNz + release + 10bnI67W9Q29udGV4dE1hcNbQCgoKPHA + PC9wPgpPR05MIENvbnRleHQ8YnI + Cgo8aW1nIHNyYz0 = "http://www.2cto.com/uploadfile/Collfiles/20140409/20140409090111183.jpg" alt = "\">
The structure diagram of OGNL Context. When struts2 accepts a request, it will quickly create ActionContext, ValueStack, and action. Then, press the action into the value stack. Therefore, the instance variables of action can be accessed by ognl. Therefore, the ognl expression can be used to access action.
ActionContext
As can be seen from the code above, the ActionContext in struts2 provides the number of ognl
Data operations. In addition, you can obtain parameters such as sessions encapsulated by struts2 through ActionContext.
ServletActionContext
You can use ServletActionContext to obtain some servlet classes, such as HttpServletRequest and ServletContext. You can use ServletActionContext to return ActionContext.
Ognl expression
1. # usage
Access OGNL context and action context, # is equivalent to ActionContext. getContext (); if the value in map is accessed, rather than the value in the object stack, the # prefix must be added during access because the data in map is not the root object.
|
Name |
Function |
Example |
|
Parameters |
Map containing the current HTTP Request |
# Parameters. id [0] = request. getParameter ("id ") |
|
Request |
Map containing the current HttpServletRequest attribute |
# Request. username = request. getAttribute ("username "); |
|
Session |
Map containing the current HttpSession attribute |
# Session. username = session. getAttribute ("username "); |
|
Application |
Map containing the current ServletContext attribute |
# Application. username = application. getAttribute ("username "); |
|
Attr |
Used to access its attributes in the order of request> session> application |
# Attr. username is equivalent to reading the attributes of username in the preceding three ranges until they are found. |
Note: You can also write it as # request ['username'] # sesssion ['username'] # application ['username']
Main steps:
In action
In the page
2. Usage 2
OGNL sets an object (root object). In struts2, the root object is CompoundRoot, or the root object in OgnlValueStack, which is usually called ValueStack (value stack or object stack ), if you want to access the attributes of the root object, you can omit # And directly access the attributes of the object.
In action
In the page
3. Usage 3 (deep understanding of object stack)
There are three classes: Person. java, Student. java, OgnlAction. java
Person class
Student Class
OgnlAction class
Create an object for the Person class and Student class and put it in the object stack. The code is:
From the previously learned OGNLContext structure, we can see the distribution in the object Stack:
Note that the student object contains the comment attribute, which is in the person object.
There is also the comment attribute, and there is also the comment attribute in OgnlAction. If the page output
What should I choose for the Comment attribute?
Conclusion:
For the attributes stored in the object stack, struts2 will start from the top, and assign values if it is found,
If it cannot be found, it will be searched for in sequence until it is found. So student should be output.
The comment value of the object.
4. Usage 4 (constructing a map)
You can also use ognl expressions to construct a Map, such as # {'foo1': 'bar1', 'foo2': 'bar2 '}.
Assign values to tags such as radio and checkbox.
1. The output on the page is as follows:
2. You can also use it like this:
5. Usage 5 (%)
The "%" symbol is used to add an OGNL expression to the execution environment % {} when the tag property value is understood as a string type.
{} Using OGNL expressions
6. Usage 6 ($)
$ Has two main purposes:
Used to reference OGNL expressions in international resource files
REFERENCE The ognl expression in the struts2 configuration file
In action:
In the configuration file:
On the page:
Tag
Property tag
Default: an optional attribute. If the output value is null, the specified value is displayed. The optional attribute of escape, which specifies whether to format it as an optional attribute of html code value and the attribute value to be output. If this attribute is not specified, the value at the top of the ValueStack stack is output by default.
Debug tag
The debug tag can be used to output all OGNLContext values.
Set tag
Var: The variable name. The name, id, and var have the same meaning. Var has replaced name, id; Scope: Specifies the range where the variable is placed. This property can accept application, session, request, page, or Action. If this attribute is not set, it is placed in action by default. Value: the Value assigned to the variable. If this attribute is not set, the value at the top of the ValueStack stack is assigned to the variable.
Push tag
Objects are placed on the top of the stack and cannot be placed in other ranges. When the Tag ends, the objects are deleted from the top of the stack.
Bean tag
Instantiate a class that complies with the javabean specification. The label body can contain several param elements, which can be used to call the set Method and assign values to the class attributes.
Name: Name of the class to be instantiated. The Name complies with the an specification. Var: the value assigned to the variable. In the request scope. If this attribute is not set, the object is set to the top of the stack.
Action tag
You can directly call the background action by specifying the namespace and action name.
Name: Namespace of Aciton Name: Namespace in which Action is located (No. action is added after the action Name) executeResult: whether the result of Action needs to be executed. The default value is false.
Iterator label
This label is used to iterate the set. The set here includes: list, set, and array.
Value: an optional attribute that specifies the set to be iterated. If this attribute is not set, the set at the top of the object stack is used. Var: Optional attribute, the name of the referenced variable Status: Optional attribute, which specifies the IteratorStatus instance during iteration. This instance contains the following methods: int getCount () returns the number of elements in the current iteration int getIndex () returns the index boolean isEven () of the elements in the current iteration () returns whether the index of the current iteration element is an even boolean isOdd (). Returns whether the index of the current iteration element is an odd boolean isFirst (). Returns whether the current iteration element is the first boolean isLast () element () returns whether the current iteration element is the last element.
If/else if/else label
Basic Process Control labels. The If tag can be used independently or in combination with the elseif or else tag.
Test: followed by the judgment expression.
Url tag
This tag is used to create a url. You can use the "param" tag to provide the request parameter.
Value: If there is no value, the current action is used. The Value must be added after the value is used. action. action: The action used to generate the url. if not, use value; Namespace: Namespace Var: name of the referenced variable.
Ognl operation set
|
Java code |
OGNL expression |
|
List. get (0) |
List [0] |
|
Array [0] |
Array [0] |
|
(User) list. get (0). getName () |
List [0]. name |
|
Array. length |
Array. length |
|
List. size () |
List. size |
|
List. isEmpty () |
List. isEmpty |
Ognl Map operation
|
Java code |
Ognl expression |
|
Map. get ("foo ") |
Map ['foo'] |
|
Map. get (new Integer (1 )); |
Map [1] |
|
User user = (User) map. get ("user ");
Return user. getName () |
Map ['user']. name |
|
Map. size () |
Map. size |
|
Map. isEmpty () |
Map. isEmpty |
|
Map. get ("foo ") |
Map. foo |
|
Java code |
Ognl expression |
|
Map map = new HashMap ();
Map. put ("foo", "bar ");
Map. put ("1", "2 ");
Return map; |
# {"Foo": "bar", "1": "2 "} |
|
Map map = new HashMap ();
Map. put (new Integer (1), "");
Map. put (new Integer (2), "B ");
Map. put (new Integer (3), "c "); |
# {1: "a", 2: "B", 3: "c "} |