Struts
2. Enhancements Based on ognl
1. Value stack (valuestack)
Set struts2 ognl context to actioncontext in struts2 (ognlcontext is used internally), and set the value stack to the root object of struts2 ognl.
We know that struts2
The root object in the ognl context can be directly accessed without any special "TAG". Other objects in the reference context must be marked. Because the value stack is the root object in the context,
Therefore, you can directly access it. How can I access the objects in the value stack? Struts2 provides a special ognlpropertyaccessor, which can automatically find
All objects (from the top of the stack to the bottom of the stack) directly find an object with the properties you are looking. That is to say, any object in the value stack can be directly accessed without using "#".
Assume that the value Stack has two objects: Student and employee. Both objects have the name attribute, student has the student ID attribute number, and employee
Salary has a salary attribute. Employee first goes into the stack, student goes into the stack, and is located at the top of the stack. For expression name, student's name attribute is accessed,
Because the student object is located at the top of the stack. The expression salary accesses the salary attribute of the employee. As you can see, the object attributes or methods in the Access value stack do not need to be
The explicit object does not need to be "#", as if the objects in the value Stack are the root objects in the ognl context. This is the improvements made by struts2 Based on ognl.
2. [N] syntax
As mentioned above, how should I write an expression if I want to access the name attribute of employee? We can use [N]. XXX (N is an integer starting from 0) to specify the position where the object is located. The expression [1]. name refers to the name attribute of the employee object.
[N]. when using the xxx syntax, pay attention to the meaning of the position sequence number. It does not mean to "get the object whose index is N in the stack", but to intercept some stacks starting from position n.
3. Top keywords
Top is used to get the objects at the top of the stack. Combined with the [N]. xxx syntax, We can get objects at any position in the stack.
For example, [0]. Top, [1]. Top, etc.
4. Access static members
In addition to using standard ognl expressions to access static fields and static methods, struts2 allows you not to specify the complete class name, instead, the "vs" prefix is used to call static fields and static methods stored in the stack.
FOO_PROPERTYsomeMethod()someMethod() |
Vs indicates valuestack. If there is only vs, the class of the stack top object will be used. If there is a number behind vs, the object class at the specified position in the stack will be used.
Standard Method: @ class fully qualified name @ static attribute or method name
< ?xml:namespace prefix = s />< s:property value="@action.SystemInfo@WEB_CONTEXT">< /s:property> |
5. Action instances in the value Stack
The struts2 framework always places the action instance on the top of the stack. Because action is in the value stack and the value stack is the root of ognl, the reference action attribute can omit the "#" mark, this is why we can directly access the attribute of action on the result page.
6. Naming objects in struts2
Struts2 also provides some named objects that are not saved in the value stack but saved in actioncontext. Therefore, the "#" mark is required to access these objects. These named objects are of the map type.
Parameters
Used to access request parameters. For example, # parameters ['id'] Or # parameters. ID is equivalent to calling the getparameter () method of the httpservletrequest object.
Note that parameters is essentially a map object constructed using the Request Parameters in the httpservletrequest object. A number of objects are created (they have been created before the action instance is called ), it has no relationship with the httpservletrequest object.
Request
Used to access request attributes. For example: # request ['user'] Or # request. User is equivalent to calling the getattribute () method of the httpservletrequest object.
Session
Used to access session attributes. For example: # session ['user'] Or # session. User is equivalent to calling the getattribute () method of the httpsession object.
Application
Used to access application properties. For example: # application ['user'] Or # application. User is equivalent to calling the getattribute () method of servletcontext.
ATTR
If pagecontext is available, access pagecontext. Otherwise, search for the request, session, and application objects in sequence.
The above is the analysis and introduction of struts2 ognl
Address: http://www.fengfly.com/plus/view-62875-1.html