OGNL Expression Language

Source: Internet
Author: User
It programmer development Essentials-all kinds of resources download list, history of the most IT resources, personal collection summary.
1.OGNL is an acronym for the object Graphic Navigation Language, an open source project. The Struts 2 framework uses OGNL as the default expression language.

2. Relative to the EL expression, it provides some of the functions we normally need, such as:

(1) Support object method invocation, such as Xxx.sayhello ();


(2) Support class static method invocation and value access, expression format is @[class full name (including package path)]@[Method name | Value name], for example: @java. Lang.string@format (' foo%s ', ' Bar ') or @cn. Itcast.constant@app_name;



(3) Manipulate the collection object.
3.OGNL has a context concept, and the context is a map structure, It implements the Java.utils.Map interface, in the context of the STRUTS2 implementation is Actioncontext, the following is the context of the structure diagram
(1) The OGNL context implementation in Struts 2 is Actioncontext, which has the following structure:


(2) When Struts2 accepts a request, the actioncontext,valuestack,action is created quickly. The action is then stored in the Valuestack, so the instance variable of the action can be accessed by OGNL.




(3) objects in the access context need to use the # notation to label namespaces, such as #application, #session

(4) Another ognl will set a root object (root object), the root object in Struts2 is the Valuestack (value stack). If you want to access the properties of an object in the root object (that is, valuestack), you can omit the # namespace and directly access the object's properties.

(5) In Struts2, the implementation class of the root object Valuestack is Ognlvaluestack, which is not what we imagine to hold a single value, but a set of objects. In the Ognlvaluestack class, there is a list type root variable that is used to store a group of objects
|--request
|--application
Context------|--ognlvaluestack root variable [action, Ognlutil, ...]
|--session
|--attr
|--parameters

(6) The first object in the root variable is called the top object of the stack. Usually we can directly write the name of the property in the OGNL expression to access the properties of the object in the root variable, the search order is from the top of the stack to find the object, if the top of the stack object does not exist this property, the second object will be looked for, if not found from the third object to find, and then go down to find.
Note: In Struts2, the OGNL expression needs to be used with the Struts label. such as: <s:property value= "Name"/>

(7) Since Valuestack (value stack) is the root object of OGNL in Struts 2, if the user needs access to objects in the value stack, the JSP page can access the properties of the object in the Valuestack (value stack) directly through the following El Expression: ${foo}// Get the Foo property of an object in the value stack

(8) If you access objects in other context, because they are not root objects, you need to add a # prefix to the access.
Application object: Used to access ServletContext, such as #application.username or #application[' userName '],  Equivalent to calling Servletcontext.getattribute ("username").  Session object: Used to access httpsession, such as #session.username or #session[' UserName '], which is equivalent to calling Session.getattribute ("UserName"). Request object: A map used to access the HttpServletRequest property (attribute), such as #request.username or #request[' userName '],  Equivalent to calling Request.getattribute ("UserName"). Parameters object: The request parameter used to access HTTP, such as #parameters.username or #parameters[' UserName '], is equivalent to calling Request.getparameter ("UserName  ")。 attr object: Used to access its properties in Page->request->session->application order.


4. Why can I use El expressions to access the properties of an object in Valuestack?






The reason is that STRUTS2 has further encapsulated the httpservletrequest. The abbreviated code is as follows: Public

 class Strutsrequestwrapper extends Httpservletrequestwrapper {public
       strutsrequestwrapper ( HttpServletRequest req) {
           super (req);
       }
       Public Object getattribute (String s) {
        ...
        Actioncontext CTX = Actioncontext.getcontext ();
        Object attribute = Super.getattribute (s);//Gets the property value from the request scope
        if (CTX! = null) {
            if (attribute = = null) {//if from Reque The St range does not find the property value, that is, the property value
               of the object from Valuestack ... Valuestack stack = Ctx.getvaluestack ();
               attribute = Stack.findvalue (s);
               ......
            }
        }
        return attribute;
    }
 }
5. Creating List/map Collection objects with OGNL expressions

If you need a collection element (such as a list object or a map object), you can use the expression associated with the collection in OGNL.
generate a list object directly using the following code:
 <s:set name= "list" value= "{' zhangming ', ' xiaoi ', ' liming '}"/>
<s: Iterator value= "#list" id= "n" >
	<s:property value= "n"/><br>
</s:iterator>

Generate a Map object:
<s:set name= "Foobar" value= "#{' foo1 ': ' bar1 ', ' foo2 ': ' Bar2 '}"/>
<s:iterator value= "# Foobar ">
	<s:property value=" key "/>=<s:property value=" value "/><br>
</s:iterator

the > Set label is used to put a value into a specified range.
Scope: Specifies the range in which the variable is placed, which can accept application, session, request, page, or action. If this property is not set, it is placed by default in the OGNL context. Value
: Values assigned to the variable. If this property is not set, the value of the top of the Valuestack stack is assigned to the variable.

6. Use OGNL expression to determine whether an object exists in the collection








For a collection type, the OGNL expression can use in and not in two element symbols. Where the in expression is used to determine whether an element is in the specified collection object, and not in to determine whether an element is in the specified collection object, as shown below.
in expression:
<s:if test= "' foo ' in {' foo ', ' Bar '} ' >
   at
</s:if>
<s:else>
   not
in </s:else>
not in expression: <s:if test= "' foo ' not in {' foo ', ' Bar '} ' >
   not
</s:if>
<s:else>
   in
</s:else>

7. Use OGNL expression to determine whether an object exists
in the collection








For a collection type, the OGNL expression can use in and not in two element symbols. Where the in expression is used to determine whether an element is in the specified collection object, and not in to determine whether an element is in the specified collection object, as shown below.
in expression:
<s:if test= "' foo ' in {' foo ', ' Bar '} ' >
</s:if>
<s:else>
   Not

in the </s:else> not expression:
<s:if test= "' foo ' not in {' foo ', ' Bar '} ' >
   not
</s:if >
<s:else>
   in
</s:else>

8. Projection function for 8.OGNL expressions








In addition to in and not, OGNL allows you to use a rule to get a subset of the collection objects, which are commonly used with the following 3 related operators.
?: Gets all the logical elements.
^: Get the first element that conforms to the logic.
$: Gets the last element that conforms to the logic.
For example code:
<s:iterator value= "books.{? #this. Price > ">
      <s:property value=" title "/> – $<s:property value=" Price "/><br>
</s:iterator>
in the code above, immediately after the collection. {} The operator indicates that a subset of the collection is used for fetching, and the expression within {} is used to get the element that matches the condition, this refers to the books to filter the data from the large collection to the small collection, and the large collection books needs to be iterated, this represents the element of the current iteration. The expression in this example is used to get a collection of books with a price greater than 35 in the collection. Public
class Bookaction extends Actionsupport {
	private list<book> books;
	....
	@Override public
    	String execute () {
        		books = new Linkedlist<book> ();
        		Books.add (new book ("A735619678", "Spring");
		Books.add (new book ("B435555322", "ejb3.0");
	}
}





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.