Custom interceptor
1). Detailed steps
I. Define an interceptor class
> Interceptor interfaces can be implemented.
> Inherit the abstractinterceptor abstract class
Ii. Define the functions of the interceptor in the Interceptor () method of the interceptor class.
Iii. Configure in the Struts. xml file.
1. register the interceptor
<Interceptors>
<Interceptor name = "hello" class = "com. atguigu. struts2.interceptors. myinterceptor"> </interceptor>
</Interceptors>
2. Use Interceptor: <Interceptor-ref name = "hello"> </Interceptor-ref>
<Action name = "testtoken" class = "com. atguigu. struts2.token. App. tokenaction">
<Interceptor-ref name = "hello"> </Interceptor-ref>
<Interceptor-ref name = "defaultstack"> </Interceptor-ref>
<Result>/success. jsp </result>
<Result name = "invalid. Token">/token-error.jsp </result>
</Action>
Iii. Note: In the custom interceptor, you can choose not to call the invoke () method of actioninvocation. The subsequent interceptor and action method will not be called.
Struts renders the result corresponding to the returned value of the custom interceptor intercept method (for example, verifying user permissions and whether the user is logged on)
Unexpected gains:
1 If you want to view the interceptor attributes in struts2, refer to the following:
<Interceptors>
<Interceptor-stack name = "atguigustack">
<Interceptor-ref name = "defaultstack">
<Param name = "fileupload. maximumsize"> 2097152 </param>
<! --
<Param name = "fileupload. allowedtypes"> text/html, text/XML </param>
<Param name = "fileupload. allowedextensions"> HTML, DTD, XML </param>
-->
</Interceptor-ref>
</Interceptor-stack>
</Interceptors>
Then change the default interceptor stack to its own interceptor stack. This step must be done. If not, struts2 cannot be loaded.
<Default-interceptor-ref name = "atguigustack"> </default-interceptor-ref>
2. If you want to use all the defined interceptor actions, you can use the following formula:
<Package name = "fileuploadtest" namespace = "/" extends = "struts-Default"> <! -- Register a custom interceptor --> <interceptors> <interceptor name = "hello" class = "com. atguigu. struts2.interceptor. App. testinterceptor"> </interceptor> <! -- Configure the Interceptor Used by all actions --> <Interceptor-stack name = "atguigustack"> <Interceptor-ref name = "hello"> </Interceptor-ref> <Interceptor-ref name = "defaultstack"> </Interceptor-ref> </Interceptor-stack> </interceptors> <! -- Use the modified interceptor Stack --> <default-interceptor-ref name = "atguigustack"> </default-interceptor-ref>
Struts2 custom interceptor