STRUTS2_VALUESTACK,OGNL detailed

Source: Internet
Author: User

Original address: http://blog.csdn.net/wyply115/article/details/8257140

First, OGNL expression

1.OGNL is an expression language used in struts2, which can be used in the tag library of a JSP to facilitate access to the properties of various objects. It is used to pass parameters of the interface to the action (and type conversions), which can also be used in STRUTS2 configuration files.

2. Root object.

OGNL is called the object Graph navigation language, the so-called object graph, that is, with any one object as the root, through OGNL can access other objects associated with this object, for example:

 Public classuser{PrivateString username; PrivateGroup Group; //Omit Setter,getter Method}   Public classgroup{PrivateString name; PrivateOrganization org; //Omit Setter,getter Method}   Public classorganization{PrivateString orgId; //Omit Setter,getter Method}

The above three classes describe the ability to navigate to a group object through the user object and navigate to the Organization object. With user root, other associated objects of this root object can be accessed through the basic getters method. "The Navigation of the object graph must be navigated by the getters method."

Attention:

* The purpose of navigation is to get the value of an object or to set the value of an object or to invoke a method of an object.

* The real purpose of the OGNL expression language is to execute Java code where Java code cannot be written, or to execute Java code more conveniently.

3.Context objects

* In the OGNL expression, it may be necessary to access a number of irrelevant objects, when we need to pass a map type of object to OGNL, the expression in the need to use the object in the map can be. And this map object is called the context.

* To access objects in the context in an expression, you need to use the syntax rules for "#对象名称"

* The main reason for the presence of a context is that multiple root objects are not supported in OGNL, and if you need to access multiple unrelated objects in an expression, you can only pass them uniformly to ognl through a map.

Second, Valuestack

1.ValueStack is an interface that uses OGNL in Struts2 (Object-graph Navigation Language) expression is actually using the class Ognlvaluestack that implements the Valuestack interface. It valuestack the default implementation class.

2.ValueStack runs through the entire action lifecycle, and each action instance has a Valuestack object that holds the current action object and other related objects.

3. Struts2 Save the Valuestack object in the request domain named: Struts.valuestack. That is, the valuestack scope is request. When the action is created, the Valuestack is created, and when the action is destroyed, Val Uestack destroyed it.

Data in 4.ValueStack is stored in two parts: root (Stack structure, compoundroot) and context (map form, ognlcontext)

(1)

* The root object is Compoundroot,compoundroot inherits the ArrayList, provides additional methods: push (), and the Pop () method, Used to access the data contained in the root object. It is because of these two methods that compoundroot becomes a stack structure.

* In Struts2, the action object itself is pressed into the valuestack (which is actually placed in the compoundroot of Valuestack) before a request is finally reached to the action method. So the action object is an element in the Compoundroot.

(2)

* The context object is Ognlcontext, which implements the map interface, in Valuestack's default implementation class, the Ognlvaluestack class, calls the method in Ongl: Ognl.createdefaultcontext ( ..) Assigning a value to the context, viewing the source code, this method returns a Ognlcontext object.

5. Three ways to get Valuestack

(1) Valuestack valuestack = Actioncontext.getcontext (). Getvaluestack ();

Actioncontext is the context of the action, which has an important 6 large object valuestack,parameters,request,session,application,attr. The OGNL expression must select an object in Acitoncontext as the root object (root), which defaults to Valuestack, so the data in the Valuestack is not added #, and the # prefix is required if you want to use another 5 objects as the root object.

(2) Valuestack valuestack = Servletactioncontext.getvaluestack (Servletactioncontext.getrequest ());

Servletactioncontext inherits the Actioncontext, which is coupled with the Web container to get some information about the Web container.

(3) Valuestack valuestack = (valuestack) servletactioncontext.getrequest (). getattribute ("Struts.valuestack");

Because Valuestack is placed in the request domain.

6.ValueStack Memory Structure diagram

The original Bovento involves the underlying implementation details, which I simplified after the original post picture (Http://img.my.csdn.net/uploads/201212/04/1354620065_9013.png)

7. Common methods of storing data

(1) data stored in the map stack, that is, the context in Valuestack.

Store data in map via request, session, etc.:

Servletactioncontext.getruquest (). SetAttribute ("username", "xxx");

Servletactioncontext.getsession (). put ("pwd", "123");

To store data directly in the map stack:

Actioncontext.getcontext (). Put ("msg", "Hello");

(2) Saving data to the object stack, that is, the root (Compoundroot) object in Valuestack

* First get root, and then press the data into root, the way is put into the bottom of the stack

Valuestack valuestack =actioncontext.getcontext (). Getvaluestack (); Valuestack.getroot (). Add (new Person ());


* First get root, use Add (index,object) to press an object into root in the specified position.

Valuestack valuestack =actioncontext.getcontext (). Getvaluestack (); Valuestack.getroot (). Add (0,New person ()); // here 0, which means pressing into the top of the stack.

* Store a map to the object stack

Valuestack valuestack =actioncontext.getcontext (). Getvaluestack (); Valuestack.set (// package into map first , put the map into the object stack and put it on top of the stack.

* Use Valuestack push method to push an object directly into the top of the stack

Valuestack valuestack =actioncontext.getcontext (). Getvaluestack (); Valuestack.push (new person ()) ;

8. Using OGNL expression to fetch data from Valuestack

(1) <s:property/>: The element that takes the top of the stack.
(2) <s:iterator/>: The element that takes the top of the stack.
Note: The Value property values for this tag can be prefixed with the # prefix (<s:select/> tag), whether from the object stack or the map stack.
Because this tag is always in the iteration, the element that is currently being iterated is placed at the top of the stack.

  

STRUTS2_VALUESTACK,OGNL Explanation (Turn)

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.