ValueStack value stack and ActionContext, valuestack

Source: Internet
Author: User

ValueStack value stack and ActionContext, valuestack

Struts2 supports the value stack (ValueStack), which can only have one root object in the OGNL context. Struts2 supports many virtual objects.

I. Value stack (ValueStack)

We can obtain the ValueStack interface object first.

// 01. Obtain the ValueStack interface object in the request. HttpServletRequest request = ServletActionContext. getRequest (); ValueStack vs = (ValueStack) request. getAttribute ("struts. valueStack ");

Breakpoint-based debugging

 

Next we use the Action class to place values to Value Stack and Stack Context respectively:

// Put it in a single column Value Stack User user = new User (); user. setAge (12); user. setUsername ("Chen Kun");. getRoot (). add (user); // put it in the double-row Stack Context (deserve to be added #). getContext (). put ("user", user );

Visit the page:

<H1> Single Column Value Stack 

Result:

We know that there is a tag in OGNL.

<s:debug></s:debug>

We use to monitor Value changes in Value Stack and Stack Context.

Obviously, we find the Value we just added in the Value Stack.

Similarly, we also find the value we just added in the Stack Context (too much content, only part of it)

 

Summary:

1. struts2 sets the OGNL context to ActionContext in Struts2 (OgnlContext is still used internally, which can be clearly seen in the figure above for debugging) and uses the value stack as the root object of OGNL. A value stack is similar to a normal Stack and conforms to the features of a later-in-first-out stack. You can put, delete, and query objects in a value stack. A value stack is the core of Struts2.

2. The value stack is defined through an interface (com. opensymphony. xwork2.util. ValueStack). The corresponding implementation class is com. opensymphony. xwork2.util. OgnlValueStack.

3. Following the value stack, the framework also places other objects in ActionContext, including Map objects that represent application, session, and request. These objects coexist in ActionContext and depend on the value stack (OGNL root object,

For example:

 

4. We know that the root object in the OGNL context can be accessed directly without any special "tag", and other objects in the reference context must be marked. Because the value stack is the root object in the context, it can be accessed directly. So how to access the objects in the value stack? Struts2 provides a special OGNLProperty Accessor, which can automatically find all the objects in the stack (from the top of the stack to the bottom of the stack) until you find an object with the attributes you have queried. That is to say, any object in the value stack can be accessed directly.

 

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.