Struts2 custom interceptor

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.