As we all know, Strut2's Action class can obtain all relevant values through attributes, such as request parameters, Action configuration parameters, passing attribute values to other Actions (through chain results), etc. To get these parameter values, the only thing we have to do is declare the property with the same name as the parameter in the Action class. Before Struts2 calls the Action method of the Action class (the default is the execute method), the corresponding Action property will be assigned.
To complete this function, Struts2 depends on the ValueStack object to a large extent. This object runs through the entire life cycle of Action (each object instance of Action class will have a ValueStack object). When Struts2 receives a .action request, it will first create an instance of the Action class object, but will not call the Action method, but will first place the corresponding properties of the Action class on the top-level node of the ValueStack object (the ValueStack object is equivalent to a stack). It is just that all attribute values are default values, such as the attribute value of String type is null, the attribute value of int type is 0, etc.
After processing the above work, Struts2 will call the interceptor in the interceptor chain. After calling all interceptors, the Action method of the Action class will be called. Before calling the Action method, the attribute value in the top-level node of the ValueStack object will be assigned to the corresponding attribute in the Action class. Everyone should pay attention, this brings us a lot of flexibility. That is to say, during the process of Struts2 calling the interceptor, the value of the attribute in the ValueStack object can be changed. When a certain attribute value is changed, the corresponding attribute value of the Action class will become the value of the last change of the attribute in the interceptor.
From the above description, it is easy to know that in the Action class of Struts2, you can obtain parameter values with the same name as the attribute, which are handled by different interceptors. For example, the interceptor that obtains the request parameters is params, and the interceptor that obtains the configuration parameters of Action is staticParams, etc. Read the corresponding values inside these interceptors and update the values of the corresponding properties of the top-level node of the ValueStack object. The ValueStack object is like a conveyor belt, passing the attribute value from one interceptor to another interceptor (of course, during this time, the attribute value may change), and finally passing it to the Action object, and the final value of the attribute in the ValueStack object is assigned to the corresponding attribute of the Action class.
When we use EL expressions in the display layer, we can not only access the properties in the 11 hidden objects in the EL expression, but also access the object property values in valueStack, because struts2 further encapsulates HttpServletRequest.
public class StrutsRequestWrapper extends HttpServletRequestWrapper {public StrutsRequestWrapper(HttpServletRequest req) {super(req);}public Object getAttribute(String s) {...... ActionContext ctx = ActionContext.getContext();Object attribute = super.getAttribute(s);//First get the attribute value from the request range if (ctx != null) {if (attribute == null) {//If the attribute value is not found from the request range, that is, the attribute value of the object is found from ValueStack... ValueStack stack = ctx.getValueStack();//Get the valueStack object attribute = stack.findValue(s);//Follow the specified attribute value from the object in valueStack...}} return attribute;}}Summarize
The above is all about this article about how Strut2 encapsulates request parameters, and I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!