OGNL Introduction
OGNL is the abbreviation of object-graph navigation Language, a powerful expression language (Expression Language, referred to as EL), which can access arbitrary properties of an object through its simple and consistent expression syntax. Call the object's method, traverse the entire object's structure diagram, realizes the field type transformation and so on function. It uses the same expression to access the properties of the object.
Xwork follows the theory that "do not invent the same wheel repeatedly", the core of its expression language is this ognl. Let's take a look at a simple example:
Remember that example of our user registration? The name of our input box is the OGNL expression, such as: User name of the input box: "<input type=" text "name=" User.username ">", after the successful registration of users we want to display user registration information, with the " <ww:property value= "User.username"/> ". Input in the input box "user.username", which resolves to the Java statement is: GetUser (). Setusername ();, "User.username" in the property label Parse to Java statement: Getuser.getusername ();.
Our two expressions are the same, but the previous one holds the value of the object property, and the latter is the value that gets the object property. Expression language is simple, understandable but powerful, more on OGNL can go to http://www.ognl.org, where there are very detailed documentation
Value Stack-ognlvaluestack
The main function of OGNL's application in the framework is to support our value stack,--ognlvaluestack, which is to access the object's properties through an expression language. The user interface enters data, which in turn saves the data to the object on which it is stacked, and the resulting data is fetched and output through an expression.
Remember the example of our user registration. Let's use a procedure to demonstrate the steps it takes to save and get data to Ognlvaluestack:
Demoregistervaluestack
Package example.register;
Import Com.opensymphony.xwork.util.OgnlValueStack;
public class Demoregistervaluestack {
public void Demo () {
Registeraction action = new registeraction ();
Ognlvaluestack valuestack= new Ognlvaluestack ();
Valuestack.push (action);
Valuestack.setvalue ("User.username", "Babydavic");
System.out.println ("username =" +valuestack.findvalue ("User.username"));
}
public static void Main (string[] args) {
Demoregistervaluestack demovaluestack = new Demoregistervaluestack ();
Demovaluestack.demo ();
}
}
Let's take a look at its demo () method:
1, create the object action of our action (Registeraction) class, pressing the action object into the stack valuestack. In Webwrok, the creation of the action, the stack is in the Defaultactioninvocation constructor, detailed introduction see: Servletdispatcher principle.
2, through the expression language, call Stack object's Get (), set () method, set the value of the object.
public void SetValue (String expr, Object value)
Statement: Valuestack.setvalue ("User.username", "Babydavic");
is equivalent to: Action.getuser (). Setusername ("Babydavic");
3, through the expression language, to the stack object to find the value we saved before, and print in the console. Valuestack.findvalue ("User.username") equals the statement:
Action.getuser (). GetUserName ()
Results of the last console printing:
Username = Babydavic
Compoundroot
In Ognlvaluestack, a stack is actually a list. View Ognlvaluestack you will find that the stack is the object of the Com.opensymphony.xwork.util.CompoundRoot class:
public class Compoundroot extends ArrayList {
~ Constructors/////////////////////////////////////
Public Compoundroot () {
}
Public compoundroot (List list) {
Super (list);
}
~ Methods////////////////////////////////////////////
Public compoundroot cutstack (int index) {
return new Compoundroot (sublist (index, size ());
}
Public Object Peek () {
Return to get (0);
}
Public Object pop () {
Return remove (0);
}
public void push (Object o) {
Add (0, O);
}
}
When we manipulate the stack object through an expression, we do not know what objects are in the stack. According to the stack from the top down order (first into the stack below, and finally into the stack at the top) in turn to find the object method to match the expression, find that the corresponding access operation. Suppose that the following object also has the same method and will not be invoked.
Here we look at a procedure for Ognlvaluestack operations, which mainly demonstrates how to access the map object and how the Ognlvaluestack stack works
Package example.register;
Import Com.opensymphony.xwork.util.OgnlValueStack;
public class Demogroupvaluestack {
public void Demoaction () {
Demogroupaction action = new demogroupaction ();
Ognlvaluestack valuestack= new Ognlvaluestack ();
Valuestack.push (action);
User Zhao = new user ();
Zhao.setusername ("Zhao");
Zhao.setemail ("zhao@yahoo.com.cn");
User Qian = new user ();
Qian.setusername ("Qian");
Qian.setemail ("qian@yahoo.com.cn");
Valuestack.setvalue ("users[' Zhao ')", Zhao);
Valuestack.setvalue ("users[' Qian ')", Qian);
System.out.println ("users[' zhao '] =" +valuestack.findvalue ("users[' Zhao ')");
System.out.println ("users[' qian '] =" +valuestack.findvalue ("users[' Qian ')");
System.out.println ("Users size =" +valuestack.findvalue ("Users.size"));
System.out.println ("allusername[0] =" +valuestack.findvalue ("allusername[0]"));
}
public void Demomodels () {
User Model_a = new user ();
Model_a.setusername ("Model_a");
User Model_b = new user ();
Model_b.setusername ("Model_b");
User Model_c = new user ();
Model_c.setusername ("Model_c");
Ognlvaluestack valuestack= new Ognlvaluestack ();
Valuestack.push (MODEL_A);
Valuestack.push (Model_b);
Valuestack.push (Model_c);
System.out.println ("username =" +valuestack.findvalue ("username"));
System.out.println ("[1].username =" +valuestack.findvalue ("[1].username"));
System.out.println ("[0].tostring =" +valuestack.findvalue ("[0]"));
System.out.println ("[1].tostring =" +valuestack.findvalue ("[1]"));
System.out.println ("[2].tostring =" +valuestack.findvalue ("[2]"));
}
public static void Main (string[] args) {
Demogroupvaluestack demovaluestack = new Demogroupvaluestack ();
Demovaluestack.demoaction ();
Demovaluestack.demomodels ();
}
}
Package example.register;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
public class Demogroupaction {
Private MAP users = new HashMap ();
Public Map getusers () {
return this.users;
}
Public List Getallusername () {
return new ArrayList (Users.keyset ());
}
Public String execute () {
Perform business operations
return null;
}
Public String toString () {
return users.tostring ();
}
}
Note: 1, access to the Map property, its expression language such as: users[' Zhao ', note that it uses ' to refer to the HashMap key string.
2. The Demomodels () method demonstrates the principle of the stack in Ognlvaluestack, paying special attention to its [0].tostring, [1].tostring, 2].tostring, which in turn calls the ToString () method of objects in the stack , and reduce the topmost object on the stack one by one.
The results of the console output are as follows:
users[' Zhao '] = username=zhao;password=null;email=zhao@yahoo.com.cn;age=0
users[' qian '] = username=qian;password=null;email=qian@yahoo.com.cn;age=0
Users size = 2
Allusername[0] = Qian
Username = Model_c
[1].username = Model_b
[0].tostring = [Username=model_c;password=null;email=null;age=0, username=model_b;password=null;email=null;age=0, Username=model_a;password=null;email=null;age=0]
[1].tostring = [Username=model_b;password=null;email=null;age=0, username=model_a;password=null;email=null;age=0]
[2].tostring = [username=model_a;password=null;email=null;age=0]