Java Knowledge Summary-9

Source: Internet
Author: User

OGNL
The language that is the object and method properties found in the object graph is a standalone feature item

OGNL is object-graph Navigation language abbreviation, it is a powerful expression language, through its simple and consistent expression syntax, can access the object's arbitrary properties, call the method of the object, traverse the whole object's structure diagram, realize the field type conversion and other functions. It uses the same expression to access the properties of the object.


OGNL Expression's form label the Name property is a OGNL expression
Non-form label all Value property is OGNL expression

The


Ognl is typically used in conjunction with the flags of Struts 2. Mostly #,%, and $ these three symbols use
------------
# "For three purposes:
Access to the OGNL context and action context,
#相当于ActionContext. GetContext ();
The following table has several properties that are useful in Acti Oncontext: The name action Example
parameters a map #parameters that contains the parameters of the current HTTP request. id[0] Function equivalent to Request.getparametervalues ( "id"). get (0); The
request contains a map #request of the properties of the current HttpServletRequest (attribute). UserName equivalent to Request.getattribute ("UserName")
The session contains the map #session of the current HttpSession property (attribute). UserName equivalent to Session.getattribute ("UserName")
Application A map #application that contains the properties of the currently applied ServletContext (attribute). UserName equivalent to Application.getattribute ("UserName")
attr Used to access its properties by request > Session > Application Order (Attribute)

1. #用于从ognl Map Collection value for the specified root (key)
2. The #attr. Username is equivalent to reading the Username property sequentially in the above three ranges (scope) until it is found for use in filtering and projection (projecting) collections, such as books. {? #this .price<100};
3. Construct a map, such as #{' foo1 ': ' bar1 ', ' foo2 ': ' Bar2 '}.
--------------
The purpose of the "%" symbol is to evaluate the value of the OGNL expression when the property of the flag is a string type.
-------------
"$" has two main uses for referencing OGNL expressions in an internationalized resource file. Configuring files in Struts 2 and i18n


Use of Expressions:
------------
1. Access to the Base object tree
The object tree is accessed by concatenating the object's references by using a dot number.
For example: Xxxx,xxxx.xxxx,xxxx. xxxx. xxxx. xxxx. xxxx
2. Access to container variables
Access to the container variable is performed by the # symbol plus an expression.
For example: #xxxx, #xxxx. xxxx, #xxxx. xxxxx.xxxx.xxxx.xxxx
3. Using the action symbol
The OGNL expression can be used in the same way as in Java operators, in addition to the use of +,-, *,/, + +,--, = =,! =, = and other operators, but also can use mod, in, No in and so on.
4. Containers, arrays, objects
OGNL supports sequential access to containers such as arrays and ArrayList: For example: Group.users[0]
At the same time, OGNL supports the search of key values for map:
Example: #session [' Mysessionpropkey ']
Not only that, OGNL also supports the expression of the container's construction:
For example: {"Green", "Red", "blue"} Constructs a list,#{"Key1": "Value1", "Key2": "value2", "Key3": "Value3"} Constructs a map
You can also create objects by using the constructor of any class object:
5. Access to static methods or variables
To refer to static methods and fields of a class, they are expressed in the same way @[email protected] or @[email protected] (args):
Example: @[email protected],@[email protected]
6. Method invocation
Directly from a Java-like method call, you can even pass parameters:
Example: User.getname (), Group.users.size (), Group.containsuser (#requestUser)
7. Projection and Selection
OGNL supports projection (projection) and selection (selection) in similar databases.
A projection is a new collection of identical properties that select each element in the collection, similar to the field operations of a relational database. The projection operation syntax is collection. {xxx}, where xxx is the public property of each element in this collection.
For example: Group.userlist. {username} will get a list of all the user's name in a group.
-----------------
The choice is to filter the collection elements that satisfy the selection condition, similar to the record operation of the relational database. The syntax for the Select operation is: Collection. {x YYY}, where X is a selection operator followed by the selected logical expression. There are three types of selection operators:
? Select all elements that meet the criteria
^ Select the first element that satisfies a condition
$ Select the last element that satisfies the criteria
For example: Group.userlist. {? #txxx. xxx! = null} will get a list of user's name in a group that is not empty.

Java Knowledge Summary-9

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.