Struts2 interceptors are similar to servlet filters. Before executing the action's Execute method, Struts2 first executes the interceptor referenced in Struts.xml, and executes the action execute method after executing the Intercept method of all the referenced interceptors.
The STRUTS2 interceptor class must inherit from the Com.opensymphony.xwork2.interceptor.Interceptor interface, and there are three methods in the Intercepter interface that need to be implemented:
void destroy();
void init();
String intercept(ActionInvocation invocation) throws Exception;
Where the Intercept method is the core of the interceptor, all installed interceptors will invoke the method. In Struts2, some self-contained interceptors have been predefined in struts-default.xml, such as timer, params, and so on. If you inherit Struts-default in the <package> tab, the current package automatically owns all the configurations in Struts-default.xml. The code is as follows:
<package name="demo" extends="struts-default" > ... </package>
There is a default reference in Struts-default.xml, and some interceptors are automatically referenced by default (that is, when the interceptor is not referenced in <action>). This default interceptor reference is as follows:
<default-interceptor-ref name= "Defaultstack"/> <interceptor-stack name= "DefaultStack" > < Interceptor-ref name= "Exception"/> <interceptor-ref name= "Alias"/> <interceptor-ref name= "ServletConfi" G "/> <interceptor-ref name=" Prepare "/> <interceptor-ref name=" i18n "/> <interceptor-ref name= "Chain"/> <interceptor-ref name= "debugging"/> <interceptor-ref name= "profiling"/> <intercept Or-ref name= "Scopedmodeldriven"/> <interceptor-ref name= "Modeldriven"/> <interceptor-ref name= "FILEUPL" Oad "/> <interceptor-ref name=" checkbox/> <interceptor-ref name= "Staticparams"/> R-ref name= "params" > <param name= "excludeparams" >dojo\. *</param> </interceptor-ref> <interceptor-ref name= "Conversionerror"/> <interceptor-ref N Ame= "Validation" > <param name= "excludemethods" >input,back,cancel,browse</param> </interceptor-ref> <interceptor-ref name= "Workflow" > <param n Ame= "Excludemethods" >input,back,cancel,browse</param> </interceptor-ref> </interceptor-stack "
The interceptor referenced above in Defaultstack can be used without reference in <action> (if any interceptor is referenced in <action>, the interceptor defined in Defaultstack will be used in the <action>, which will be explained in detail later.