Valuestack and Struts2valuestack in Struts2
ValueStack)
Struts2 sets the OGNL context to ActionContext in Struts2.
OgnlContext), and set the value stack as the root object of OGNL.
We know that the root object in the OGNL context can be accessed directly without any special "tag ",
Other objects in the reference context must be marked. Because the value stack is the root pair in the context
And can be accessed directly. How can I access the objects in the value stack? Struts2 provides
OGNLPropertyAccessor, which can automatically search for all objects in the stack (from the top of the stack to the stack)
Directly find an object with the properties you are looking. That is to say, for any pair
Xiang can be accessed directly without using "#".
Assume that the value Stack has two objects: student and employee. Both objects have the name attribute and student has
Student ID attribute number, while employee has salary attribute salary. Employee first enters the stack, student then
Stack is located at the top of the stack. For the expression name, the name attribute of student is accessed, because
The student object is located at the top of the stack. The expression salary accesses the salary attribute of the employee. As
As you can see, accessing the object attributes or methods in the value stack does not need to specify the object or "#", just like the value Stack
All objects in are the same as the root objects in the OGNL context. This is the improvements made by Struts2 Based on OGNL.
Action instances in the value Stack
The Struts2 framework always places the Action instance on the top of the stack. Because Action is in the value stack, and the value stack is OGNL
So the "#" mark can be omitted for the property of the referenced Action, which is why we
Causes for direct access to the Action attributes.
Name object in Struts2
Struts2 also provides some named objects that are not saved in the value stack, but saved in
Therefore, the "#" mark is required to access these objects. All these named objects are Map objects.
Type.
Parameters
Used to access request parameters. For example: # parameters ['id'] Or # parameters. id, which is equivalent to calling
The getParameter () method of the HttpServletRequest object.
Note that parameters is constructed using the Request parameters in the HttpServletRequest object.
Map object. Once the object is created (it has been created before the Action instance is called ),
The HttpServletRequest object does not have any relationship.
Request
Used to access request attributes. For example: # request ['user'] Or # request. user, which is equivalent to calling
The getAttribute () method of the HttpServletRequest object.
Session
Used to access session attributes. For example: # session ['user'] Or # session. user, which is equivalent to calling
The getAttribute () method of the HttpSession object.
Application
Used to access application properties. For example: # application ['user'] Or # application. user
When the getAttribute () method of ServletContext is called.
Attr
If PageContext is available, access PageContext. Otherwise, search for request, session, and
Application object.
Here is the conversion: first, we should clearly distinguish ActionContext, ValueStack, and Stack Context.
ActionContext
One Action call creates an ActionContext.
Call: ActionContext context = ActionContext. getContext ()
ValueStack
Implemented by the OGNL framework
You can simply think of it as a stack (List ).
Stack Object: The Object put into the stack, usually action.
Stack Context (map): stack Context, which contains a series of objects, including request/session
/Attr/application map.
EL: access any property of an object, call the object method, and traverse the entire object...
ActionContextIs the Action context. You can obtain the requestsession application.
ValueStack is the value in the value stack storage form.
Stack Context is also used to store values.
Struts2 further expands the concept of OGNL context. In struts2, The OGNL context is usually as follows:
As shown below:
| -- Request
|
| -- Application
|
Context map --- | -- OgnlValueStack (root) [user, action, OgnlUtil,...]
|
| -- Session
|
| -- Attr
|
| -- Parameters
In Struts2, the standard naming Context is used to process OGNL expressions. Process OGNL
The top-level object is a Map (also called context map), and OGNL is a top-level object in this context.
Level object (root ). In terms of usage, the attribute access of top-level objects does not require any prefixes. However
It is not a top-level Object Access and must be marked.
The Struts2 framework sets OGNL Context as our ActionContext. And ValueStack
Is the root object of OGNL. In addition to value stack, the Struts2 framework also represents application,
The Map objects of the session and request objects are also placed in the ActionContext. (That is
Struts2 recommends that you do not directly access the ServletAPI in the Action class.
The ActionContext object partially replaces these (Servlet API) functions to facilitate the Action class
Test !)
Action instances are always placed in value stack. Because Action is placed in the stack, and stack
Is root (root object), so access to the attribute in Action can be omitted # mark. However
In ActionContext, the attributes of other objects must be marked with # To make OGNL know, not from
Root object, but from other objects.
The code for accessing the attribute in the Action can be written in this way.
<S: propertyvalue = "postalCode"/>
Access to non-root object attributes in other ActionContext should be written as follows:
<S: propertyvalue = "# session. mySessionPropKey"/> or
<S: propertyvalue = "# session ['mysessionpropkey']"/> or
<S: property value = "# request ['myrequestpropkey']"/>
Processing the Collection, the content is very simple.
<S: select label = "label" name = "name" list = "{'name1', 'name2', 'name3 '}"
Value = "% {'name2'}"/>
This is the processing List. This Code creates a drop-down option on the page with the content in the list,
The default value is name2.
Process map
<S: select label = "label" name = "name" list = "# {'foo': 'foovalue ',
'Bar': 'barvalue'} "/>
It should be noted that you can determine whether a value is in the collection. We want to use in or not in
.
<S: if test = "'foo' in {'foo', 'bar'}">
Muhahaha
</S: if>
<S: else>
Boo
</S: else>
In addition, you can use wildcards to select a subset of collection objects.
? -- All elements matching the selection Logic
^ -- Extract only the first element that meets the selection Logic
$ -- Extract only the last element that matches the selection Logic
Person. relatives .{? # This. gender = 'male '}
'''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''
The following are some of the further excerpts:
Q: In Struts2, how does one use its own Tag to read the variable in the Action?
The Struts2 Tag will find the corresponding pair in ValueStack according to the OGNL expression in the value.
Image. Because action is at the top of ValueStack, The OGNL table in the Tag of Struts2 is used by default.
To search for the variables in the action. Please note that the content in value is directly an OGNL expression and does not require any
He el's label packaging.
Example: <s: property value = "user. name"/>
Question: In Struts2, how to use its own Tag to read HttpServletRequest and HttpSession
Variable in?
In the above knowledge, we know that the OGNL context in Struts2 contains
Map encapsulation of servlet objects such as request, session, and application. Since these objects are
In the context of OGNL, based on the basic knowledge of OGNL, we can add the # symbol before the expression.
To access the values of these variables.
Example: <s: propertyvalue = "% {# application. myApplicationAttribute}"/>
<S: propertyvalue = "% {# session. mySessionAttribute}"/>
<S: property value = "% {# request. myRequestAttribute}"/>
<S: propertyvalue = "% {# parameters. myParameter}"/>
Q: In Struts2, how does one use JSTL to read the variable in Action?
This is a long history issue. Because in fact, many friends (including me) do not use
Struts2 uses JSTL instead of its own tag library, probably because JSTL has fewer tag libraries and is easy to use.
.
We know that JSTL uses pages, requests, sessions, and applications by default.
Find the value of the object corresponding to the corresponding EL expression. If you want to use JSTL to read the changes in the Action
The variable in the Action must be placed in the request field. Therefore, as early as Webwork2.1.X
We will compile an interceptor to do this. The general principle is: after the Action is executed
Read all the variables in the Action and call request. setAttribute ()
. For specific integration methods, refer to the following article
File: http://wiki.opensymphony.com/display/WW
/Using + WebWork + and + XWork + with + JSP + 2.0 + and + JSTL + 1.1
However, with the development of the times, the above method is no longer recommended. (Even so, I
Currently, since Webwork2.2, including
Struts2 uses another Integration Method: decoration of HttpServletRequest. Let's come
Let's take a look at the source code:
Java code
1.public class StrutsRequestWrapper extends HttpServletRequestWrapper { 2. 3. /** 4. * The constructor 5. * @param req The request 6. */ 7. public StrutsRequestWrapper(HttpServletRequest req) { 8. super(req); 9. } 10. 11. /** 12. * Gets the object, looking in the value stack if not found 13. * 14. * @param s The attribute key 15. */ 16. public Object getAttribute(String s) { 17. if (s != null && s.startsWith("javax.servlet")) { 18. // don't bother with the standardjavax.servlet attributes, we can short-circuit this 19. // see WW-953 and the forums postlinked in that issue for more info 20. return super.getAttribute(s); 21. } 22. 23. ActionContext ctx = ActionContext.getContext(); 24. Object attribute = super.getAttribute(s); 25. 26. boolean alreadyIn = false; 27. Boolean b = (Boolean)ctx.get("__requestWrapper.getAttribute"); 28. if (b != null) { 29. alreadyIn = b.booleanValue(); 30. } 31. 32. // note: we don't let # come through or else a request for 33. // #attr.foo or #request.foo could cause an endless loop 34. if (!alreadyIn && attribute == null &&s.indexOf("#") == -1) { 35. try { 36. // If not found, then try theValueStack 37. ctx.put("__requestWrapper.getAttribute", Boolean.TRUE); 38. ValueStack stack =ctx.getValueStack(); 39. if (stack != null) { 40. attribute =stack.findValue(s); 41. } 42. } finally { 43. ctx.put("__requestWrapper.getAttribute", Boolean.FALSE); 44. } 45. } 46. return attribute; 47. } 48. }
Why? This class replaces HttpServletRequest during Struts2 initialization and runs on
During the entire Struts2 operation, when we try to call request. getAttribute (),
Will execute the above method. (This is a typical decorator mode) When you execute the above method
First, call the original request. getAttribute () in HttpServletRequest. If it is not found,
It will continue to look up in ValueStack, while action is in ValueStack, so the variable in action
The corresponding value can be found through the OGNL expression.
Here, el expressions are widely used today. After JSTL1.1, el expressions can also be used directly.
Note the difference between struts2 and struts2. Here the el symbol is used: $ {}
Example: $ {user. name}, <c: out value = "$ {department. name}"/>
Question: In Struts2, How to Use templates such as Freemarker to read the variables in the Action and
Variables in HttpServletRequest and HttpSession?
Templates such as Freemarker have corresponding results in Struts2, and among these results, Freemarker
Templates construct the identifiable
Model, so that the template can use their respective syntax
Read.
For more information about Freemarker reading variables, see the official documentation of Struts2.
Fine: http://struts.apache.org/2.0.14/docs/freemarker.html
Set Value Calculation
In Struts2, OGNL is used for value setting calculation, which means that the View layer transmits data to the Control layer and can
Set to the corresponding Java object. Logically, this process needs to be completed in two steps:
1. For each request, an ActionContext corresponding to the corresponding Action is created as the upstream and downstream of OGNL.
File environment and ValueStack, and press the Action into ValueStack
2. Before a request enters the Action Code, use a common mechanism to collect the parameters uploaded by the page.
And call OGNL-related code to set the value of the Action.
The first step is completed when processing the URL request, and the second step is implemented by the built-in interceptor of struts2.
Complete.
Address: http://blog.csdn.net/djx123456/article/details/6794581