Actioncontext
An action call will create a Actioncontext
Call: Actioncontext context = Actioncontext.getcontext ()
Valuestack
Implemented by the OGNL framework
You can simply think of it as a stack (List).
Stack object: An object that is placed in a stack, typically an action.
Stack context (map): Stack contexts, which contain a series of objects, including Request/session/attr/application map, and so on.
EL: Access Any property of the object, invoke the object's method, traverse the entire object knot ...
Actioncontext is the action context and can get the request session application
Valuestack is the value of the value stack that is stored in the form
The context of a stack context stack is also used to store values
Struts2 further expands the concept of the OGNL context, in Struts2, where the OGNL context is usually as follows:
|--request
|
|--application
|
Context Map---|--ognlvaluestack (root) [user, Action, Ognlutil, ...]
|
|--session
|
|--attr
|
|--parameters
In Struts2, the OGNL expression is processed using a standard naming context (contextual). The top-level object that handles OGNL is a map (also called a context map), and OGNL is a top-level object (root) in this context. In usage, the top-level object's property access is not required for any tag prefixes. Other non-top-level object accesses require the use of the # tag.
The STRUTS2 framework sets the OGNL context to our actioncontext. And valuestack as the root object of the OGNL. In addition to the value stack, the STRUTS2 framework also places map objects representing application, session, and request objects in Actioncontext. ( This is why STRUTS2 recommends that you do not directly access the Servlet API in the action class, and that he can partially replace these (Servlet API) functions with Actioncontext objects to make it easier to test the action Class! )
An instance of the action is always placed in the value stack. Because the action is placed in the stack and the stack is root (the root object), access to the properties in the action can omit the # tag. However, to access the properties of other objects in Actioncontext, it is necessary to bring the # tag, so that Ognl knows, not from the root object, but from other objects.
Then the code that accesses the properties in the action can write
<S:propertyvalue= "PostalCode"/>access to non-root object properties in other Actioncontext is written like this:<S:propertyvalue= "#session. Mysessionpropkey"/>or<S:propertyvalue= "#session [' Mysessionpropkey ']"/>or<S:propertyvalue= "#request [' Myrequestpropkey ']"/>The content of the collection is very simple. <S:selectlabel= "Label"name= "Name"List= "{' name1 ', ' name2 ', ' Name3 '}"value= "%{' name2 '}" />This is the process list. This code creates a drop-down option on the page that contains the contents of the list, and the default value is name2. Working with Map<S:selectlabel= "Label"name= "Name"List= "#{' foo ': ' Foovalue ', ' bar ': ' Barvalue '}" />It is important to note that a value is determined in collection. We are going to use in or not to handle. <s:ifTest= "' foo ' in {' foo ', ' Bar '}">Muhahaha</s:if> <S:else>Boo</S:else>Alternatively, you can use wildcards to select a subset of the collection objects.?—— all elements that match the selection logic ^--extracts only the first element that conforms to the selection logic $--extracts only the last element that conforms to the selection logic person.relatives. {? #this. Gender = = ' Male '}
The following are some of the questions that add to the excerpt:
1. Question: In Struts2, how do I use its own tag to read the variables in the action?
Struts2 's own tag will look for the corresponding object in Valuestack based on the OGNL expression in value. Because the action is at the top of the Valuestack, by default, the OGNL expression in STRUTS2 's tag looks for the variable in the action. Note that the content in value is directly the OGNL expression, without any El tag wrapping.
Example: <s:property value= "User.Name"/>
2. Question: In Struts2, how do you use your own tag to read the variables in the httpservletrequest,httpsession?
In the above knowledge, we know that in the context of OGNL in Struts2, a map package containing servlet objects such as Request,session,application is included. Now that these objects are in the context of OGNL, we can access the values of these variables by adding the # symbol before the expression, based on the basic knowledge of OGNL.
Example: <s:property value= "%{#application. Myapplicationattribute}"/>
<s:property value= "%{#session. Mysessionattribute}"/>
<s:property value= "%{#request. Myrequestattribute}"/>
<s:property value= "%{#parameters. Myparameter}"/>
3. the difference between Valuestack and stack context in popular struts2.
Valuestack is implemented by the OGNL framework, which can be simply viewed as a stack (List) stack context (save as a map type): The context of the stack, which contains a series of objects, including Request,session,attr, The values stored in the valuestack, such as Application,map, can be taken directly, and the stack needs to be preceded by # (request,session,application)
The bottom line is that both can store values, and a struts package suggests a way to preserve the original SERVLETAPI.
struts2.1.6 Tutorials Four-two, OGNL and Valuestack (VS) (Actioncontext, Valuestack, Stack Context