Introduction OGNL

OGNL description:
OGNL is Object-Graph Navigation Language abbreviation, it is a powerful expression language, through its simple and consistent expression syntax, you can access any of the properties of the object, the object method calls to traverse the entire structure diagram objects, field type conversion functions to realize. It uses the same expression to access attributes of the object. This allows for better data acquisition.
OGNL allows us to use very simple expressions to access the object layer, for example, the current environment is the root object user1, expression person.address [0] .province person has access to the property user1 first address the province Properties .
This function is an important complement to the template language, like jsp2.0, velocity, jelly and so on, have similar functions, but is much more perfect than they ognl, but also with a separate lib appears to help us build their own framework .
expression:
OGNL supports a variety of complicated expressions. But the most basic prototype expression, is the object reference point values ​​in series, from left to right, each calculation result of the expression returns the current object becomes, later on followed by calculation of the current object until all expressions calculations are complete, the resulting object is returned. OGNL is carried out for this basic principle of continuous expansion, thus making support, access to the object tree, an array of containers, and even the selection SQL-like projection in the operation.
1. Basic access object tree
Access is through the use of object tree point number will be referenced in series object.
例如:xxxx,xxxx.xxxx,xxxx. xxxx. xxxx. xxxx. xxxx
2. Access to container variables
Access to container variables, coupled with expressions by the # sign.
例如:#xxxx,#xxxx. xxxx,#xxxx.xxxxx. xxxx. xxxx. xxxx
3. Operation Symbol
OGNL expressions that can be used with essentially the same operator in Java operators, in addition to using the +, -, *, /, +, -!, ==, =, = and other operators outside, but also use mod, in, not in and so on.
4. The container, arrays, objects
OGNL support sequential access to the array of containers and the like ArrayList: example: group.users [0]
Meanwhile, OGNL find support for key values ​​of Map:
例如:#session['mySessionPropKey']
Not only that, OGNL also supports the expression construct container:
For example: { "green", "red", "blue"} a configuration List, # { "key1": "value1", "key2": "value2", "key3": "value3"} a configuration Map
You can also create a new target by the constructor of any class of the object:
For example: new Java.net.URL ( "xxxxxx /")
5. access to static methods or variables
To reference a static class methods and fields, and their expression is the same @ class @ member or @ class @ method (args):
例如:@com.javaeye.core.Resource@ENABLE,@com.javaeye.core.Resource@getAllResources
6. Method Invocation
Directly by calling a manner similar to Java methods, you can even pass parameters:
例如:user.getName(),group.users.size(),group.containsUser(#requestUser)
7. projection and selection
OGNL support projection (projection) and selection (selection) similar to the database.
Projection is the same set of selected properties of each element to form a new set, the operation similar to the fields in a relational database. The syntax for the projection operation collection. {XXX}, where XXX is the common set of attributes for each element.
For example: group.userList {username} group and obtain a list of all the user's name.
Option is to meet the selection criteria to filter the set of elements, similar to the record operating relational database. The syntax for selecting operation: collection {X YYY}, where X is a selected operator, the back is selected with the logical expression. Selecting the operator in three ways:
? Select all the elements to meet the conditions
^ Selected to meet the conditions of the first element
$ Select to meet the conditions of the last element
For example:. {?! # Txxx.xxx = null} group.userList will get a group in user's name is not empty list of user.
 

Ognl is actually Map, it is divided into a root object (Root) and a number of non-root object
non-root object through "#key" access, root object can be omitted "#key"
For example, we get an object that is the root object attribute, you only need to direct the like based on the attribute name: String employeeName = (String) OnglExpression.getValue ( "name", ctx, e);
if it is not the root object: String managerName (String) OnglExpression.getValue ( "# manager .name ", ctx, e);

 

Target value and the value of the non-root object phenomenon, as well as the values ​​and assignment of cases OGNL

 1 package com.xy.test;
 2 
 3 import ognl.OgnlContext;
 4 import ognl.OgnlException;
 5 
 6 public class Demo1 {
 7 
 8     /**
 9      * @param args
10      * @throws OgnlException
11      */
12     public static void main(String[] args)  {
13         Employee e = new Employee();
14         e.setName("小李");
15 
16         M = Manager new new Manager ();
 . 17          m.setName ( "Zhang" );
 18 is  
. 19          // create OGNL Hereinafter, the context is actually a OGNL Map object 
20 is          OgnlContext CTX = new new OgnlContext ();
 21 is  
22 is          // will employees and managers were to go into the context OGNL 
23 is          ctx.put ( "employee" , E);
 24          ctx.put ( "Manager" , m);
 25          ctx.setRoot (E); // set the root object's context OGNL 
26  
27          / ** ********************************************************** value operation ********************* ****** * / 
28          //Expressions name will be executed e.getName (), because the e object is the root object (Please note the difference between root and non-root object Object expression) 
29          String employeeName = (String) OnglExpression.getValue ( "name" , ctx, e) ;
 30          System.out.println (employeeName);
 31  
32          // expression # manager.name will perform m.getName (), Note: If the access is not the root object you must add a name space in the front, for example: # manager.name 
33 is          String managerName = (String) OnglExpression.getValue ( "# manager.name" ,
 34 is                  CTX, E);
 35          System.out.println (managerName);
 36  
37 [          // course root object may also be used #employee. name expressions visit 
38          employeeName = (String) OnglExpression.getValue ( "# employee.name" , ctx,
 39                 e);
40         System.out.println(employeeName);
41 
42         /** ********************** 赋值操作 *************************** */
43         OnglExpression.setValue("name", ctx, e, "小明");
44         employeeName = (String) OnglExpression.getValue("name", ctx, e);
45         System.out.println(employeeName);
46 
47         OnglExpression.setValue("#manager.name", ctx, e, "孙经理");
48         managerName = (String) OnglExpression.getValue("#manager.name", ctx, e);
49         System.out.println(managerName);
50 
51         OnglExpression.setValue("#employee.name", ctx, e, "小芳");
52         employeeName = (String) OnglExpression.getValue("name", ctx, e);
53         System.out.println(employeeName);
54     }
55 
56 }

OGNL push to ValueStack

Background custom properties and achieve modelDriver interface that provides get and set methods

 1 private HttpServletRequest req;
 2     private Cal cal1=new Cal();
 3     private Cal cal2;
 4     private String sex;
 5     private String num1;
 6     
 7     
 8     public String getNum1() {
 9         return num1;
10     }
11 
12 
13     public void setNum1(String num1) {
14         this.num1 = num1;
15     }
16 
17 
18     public Cal getCal2() {
19         return cal2;
20     }
21 
22 
23     public void setCal2(Cal cal2) {
24         this.cal2 = cal2;
25     }
26 
27 
28     public String getSex() {
29         return sex;
30     }
31 
32 
33     public void setSex(String sex) {
34         this.sex = sex;
35     }
36     
37     /**
38      * implements ModelDriven
39      * @return
40      */
41     public String accept1() {
42         System.out.println("cal1:"+cal1);
43         System.out.println("num1:"+num1);
44         return "rs";
45     }

Run display cal1.num1 have value, but they did not get value num1

This ValueStack a relationship and push, and push the structure is last out, but ValueStack values is to take the value will return, will not take a down.
The ValueStack to push the value of the first xxxAction is what we value by way of the first set and get methods pushed onto the stack at the end, then put ModelDriver pushed, will first of all get to be obtained by ModelDriver value, acquired to return, causing the value of the latter will not be acquired.

Guess you like

Origin www.cnblogs.com/AluoKa/p/11104625.html