1: Simply put, the value stack is a lightweight data storage center corresponding to each request object, where data is managed uniformly for other parts of Struts 2 such as Action, Result, Interceptor and so on, so that data is managed centrally without clutter.
Simply put, the value stack can provide a common data access service for each request thread-safe.
Struts2 creates a new value stack for each request when there is a request, that is to say, the stack and the request are one-to-one, and the value stack is different for different requests, and the value stack encapsulates all relevant data that needs to be operated on once.
It is precisely because of the corresponding relationship between the value stack and the request that the value stack can guarantee thread safety and provide a common data access service for each request.
2: Narrow Value Stack
Commonly refers to the object of the com.opensymphony.xwork2.util.ValueStack interface, which is currently the object of com.opensymphony.xwork2.ognl.OgnlValueStack.
Narrow-sense value stack is mainly used to store the values and results needed by dynamic EL (expression language) operations, while OgnlValueStack objects are mainly used to support OGNL (object Graph Navigation language) operations.
The narrow value stack contains some data that OGNL can access, as follows:
a: An instance of action so that the value of the attribute in the action instance can be accessed through OGNL.
b: The value of OGNL expression operation can be set to the value stack, and the value stack object can be accessed actively and set forcibly.
c: Intermediate variables generated by OGNL expressions, such as cyclic tags when using Struts 2 tags, naturally have cyclic variables, which are placed on the value stack.
3: Generalized value stack
Usually an ActionContext object. ActionContext is the context in which an Action runs. Each ActionContext is a basic container that contains the data needed by the Aciton to run, such as request parameters, sessions, etc.
ActionContext is also thread-safe, with each thread having a separate ActionContext, so you don't have to worry about thread-worthy security issues in the value stack.
ActionContext stores many values, as follows:
a: Parameters of Request, the parameters in the request. Note that the data here is copied from the data object, so the changes in the data here will not affect the values of the parameters in the request object.
b: Attribute of Request, attribute of request, here is a Map, which stores attribute data of request object, which is linked with Attribute of request object.
c: Attribute of Application, Attribute of Application, here is a Map, which stores attribute data of application object. These data and attributes of application object are linked.
d: ValueStack, or narrow value stack, ActionContext is based on value stack as the root of access by OGNL. In short, OGNL accesses the value stack without specifying it specifically.
e: attr, get values in all attribute ranges, search page, request, session and application in turn
4: Use of ActionContext
Acquisition, in two ways, the first is to use the ActionContext's own method of acquisition
ActionContext ctx = ActionContext.getContext();
Second, use Action Invocation to obtain
ActionContext ctx = actionInvocation.getInvocationContext();
The typical methods are as follows:
Objectget(String key):Returns a value that is stored in the current ActionContext by doing a lookup using the value's key. void put(String key,Object value):Stores a value in the current ActionContext. Map<String,Object>getApplication(): Returns a Map of the ServletContext when in a servlet environment or a generic application level Map otherwise. //That is, to return the value returned in ServletContext Map<String,Object>getSession(): Gets the Map of HttpSession values when in a servlet environment or a generic session map otherwise. Map<String,Object>getContextMap(): Gets the context map. Map<String,Object>getParameters(): Returns a Map of the HttpServletRequest parameters when in a servlet environment or a generic Map of parameters otherwise.
5: Use of ValueStack
ValueStack has a feature that if there are multiple objects in the value stack accessed and the same attributes appear in multiple objects at the same time, the value stack will search for the first matching object in the order from the top of the stack to the bottom of the stack.
Getting ValueStack: It can be obtained directly by the getValueStack () method of the ActionContext object.
Use functions:
ObjectfindValue(String expr): Find a value by evaluating the given expression against the stack in the default search order. voidsetValue(String expr,Object value): Attempts to set a property on a bean in the stack with the given expression using the default search order. Object peek(): Get the object on the top of the stack without changing the stack. Objectpop(): Get the object on the top of the stack and remove it from the stack. voidpush(Object o): Put this object onto the top of the stack
6: For example, modify the parameter information entered by the user, as shown in the following figure.
Figure: User enters aa's username
Figure: username attribute values changed after user submission
Realization:
First, define a class that implements the PreResultListener interface: MyPreResultListener
import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.PreResultListener; public class MyPreResultListener implements PreResultListener { @Override public void beforeResult(ActionInvocation invocation, String resultCode) { System.out.println("Processing now Result Pre-execution functions, result=" + resultCode); //Modify the username value in the value stack before Result processing invocation.getInvocationContext().getValueStack().setValue("username", "Modified"); } }
Then register in the corresponding Action:
import com.capinfotech.listener.MyPreResultListener; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class PreResultAction extends ActionSupport { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String execute() { System.out.println("The parameters entered by the user are,username:" + username + ", password:" + password); ActionContext context = ActionContext.getContext(); MyPreResultListener preListener = new MyPreResultListener(); context.getActionInvocation().addPreResultListener(preListener); return "success"; } }