Detailed description of interceptor in Struts2
What is the interceptor?
An Interceptor is a mechanism that allows users to process functions before and after the Action is executed.
The sequence of the interceptor before the action is executed is the opposite of that after the result is executed. You can see:
That is, the execution order is: Interceptor1-Interceptor2-Interceptor3-Action-Result-Interceptor3 -- Interceptor2 -- Interceptor1
Advantages of interceptor
Below is the summary of the advantages of the book, posted a few images, or faster than writing your own notebook.
What are the interceptors?
When we use Action, we find that our domain value in action already has a value, and these values are the same as the parameters requested by the user. This indicates that before executing execute, someone has done this thing. It is the interceptor. Interceptors with these default configurations. Become a predefined interceptor.
Predefined interceptor
We can view in the default-struts.xml file:
。。。。。。。
dojo\..*,^struts\..*
(1) params interceptor
This interceptor is required because it sets the request parameter to the Action attribute of the attribute and converts the type.
Next, let's take a look at how this interceptor is implemented:
View the source code of ParametersInterceptor. The official explanation is:
(1)This interceptor sets all parameters on the value stack.(2)This interceptor gets all parameters from ActionContext than ,getParameters() and sets them on the value stack by calling ValueStack.setValue(String, Object), typically resulting in the values submitted in a form request being applied to an action in the value stack. Note that the parameter map must contain a String key and often containers a String[] for the value.
The interceptor obtains the request parameters from the ActionContext, and then sets the value to the value stack through ValueStack. setValue (String, Object.
// Allowed names of parameters private String acceptedParamNames = "\\w+((\\.\\w+)|(\\[\\d+\\])|(\\(\\d+\\))|(\\['\\w+'\\])|(\\('\\w+'\\)))*"; private Pattern acceptedPattern = Pattern.compile(acceptedParamNames); private ValueStackFactory valueStackFactory; @Inject public void setValueStackFactory(ValueStackFactory valueStackFactory) { this.valueStackFactory = valueStackFactory; }
The above Code shows that the interceptor defines acceptable parameter types by using regular expressions, and has a ValueStackFactory to store data in valueStack, for more information, see the source code.
(2) staticParams
This interceptor sets the Action parameter defined in the struts. xml configuration file to the corresponding Action instance,
test
As mentioned above, there is a param In the struts. xml file, which also requires that Action be used for the attributes of an account and has get and set methods.
Note:We have found that both params interceptor and staticParams interceptor are used to add attributes to action. In this case, our request field contains parameters, and we are also in struts. if parameters are configured in the xml file, what is the final result?
In fact, after the Action is initialized, the struts. the configuration data in the xml file is set to the corresponding attribute area in the action instance, and the data requested by the user is set to the attribute in the Action instance. Obviously, the final value should be the account data in the user request, that is, it is overwritten in struts. Attributes configured in the xml file.
Sequence of interceptor calls
Example:
test
/welcome.jsp
There is no interceptor in this configuration. He does not reference the life interceptor himself, and his reports do not reference the default life interceptor, however, the default interceptor reference, that is, defaultStack, is declared in the struts-default parent package. Therefore, this action has this default interceptor.
test
/welcome.jsp
If an interceptor is referenced in the above code, it will overwrite the default reference interceptor defined in the parent package. Only this interceptor works.
For this overwrite relationship, we need to add a default reference to the action, as shown below:
test
/welcome.jsp