Struts2 Interceptor Depth Interpretation

Source: Internet
Author: User
Tags http request
Interceptors are the struts2 core, and many functions are implemented by interceptors.

I. Write a interceptor yourself



Write the interception in the Interceptor method





two. Configuring Interceptors in Struts2.xml

Write the <interceptor></interceptor> interceptor definition first, and then add the <interceptor-ref> reference after the action's result



Note
The interceptor in the STRUTS2 is similar to the servlet filter, but the Struts2 interceptor can only be added in front of the action because it is configured in front of the action's result
The simple thing is that your interceptor inherits Abstactinterceptor.

Three. Method Interceptors (interception of specified methods)Methdfilterinterceptor

The above interceptor intercepts the Execute method in action, then how to intercept it by writing its own method.

1. Write a interceptor and then inherit the Methodfilterinterceptor class


2. Configure the method interceptor in Struts.xml to specify the interception method:





four. Using interceptors to verify user login

1). Write a login interceptor in addition to the login method to determine whether the user login

2). This interceptor is defined so that it should be used before each action method except for landing.

The above method return Action.login so in Struts.xml need to configure a gloabal-results name for login


Define a Interceptor-stack



Five. Executeandwaitinterceptor

When the HTTP request takes a long time, use this interceptor (on the one hand, let the user see the Waiting page, on the other hand, automatically refresh the request);
Configured as follows
1). Configure the Execandwait interceptor in the Struts.xml


2). The Handwriting wait.jsp



Six. The above interceptor interceptor can only intercept the action and cannot intercept the JSP page, so the servlet filter is required.

1). Define a filter of your own



2). Configure in Web. xml



--for verification, etc. operations





Reprinted from: http://blog.sina.com.cn/s/blog_aaa756740101hudd.html

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.