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.