Struts2 re-learning custom Interceptor (determine whether the user is logged on)

Source: Internet
Author: User

Interceptor

1: 1: concept: the interceptor is similar to the filter we have learned. It is the code that can be executed before and after the action is executed. Is a common technology used in Web development. For example, permission control and logging.

2: Multiple interceptor are connected together to form an interceptor stack. Interceptor is an implementation of AOP for Aspect-Oriented Programming and has the hot swapping effect.

3: struts2 interceptor, each interceptor class has only one object instance, that is, the single-profit mode. All actions that reference this interceptor share an instance of the interceptor class.

 

Differences between interceptor and filter

1: The concept of interceptor and filter is very similar.

2: The filter belongs to the Web Container and can filter all requests (including action, Servlet, JSP, html)

3: The Interceptor is affiliated with the struts2 framework and can only intercept action (cannot intercept direct requests to JSP)

4: function callback is used inside the filter, and dynamic proxy + recursive call is used for the interceptor.

 

Customize an interceptor. Checks whether the user is logged on. If the user is logged on, no operations are blocked. If the logon status is not logged on, or the logon status remains static for a long time, resulting in session death, any operation will be blocked (except the operation being logged on ). Go to the logon page to log on.

I. User-Defined login status judgment interceptor

1 package COM. bjsxt. shangxiaofei. interceptor; 2 3 Import Java. util. map; 4 5 import COM. bjsxt. shangxiaofei. po. user; 6 Import COM. opensymphony. xwork2.actioncontext; 7 Import COM. opensymphony. xwork2.actioninvocation; 8 Import COM. opensymphony. xwork2.interceptor. interceptor; 9 10 public class logininterceptor implements interceptor {11 12 @ override13 public void destroy () {14 // todo auto-generated meth Od stub15 system. out. println ("logininterceptor. destroy (destroy) "); 16} 17 18 @ override19 public void Init () {20 // todo auto-generated method stub21 system. out. println ("logininterceptor. init (initialization) "); 22} 23 24 @ override25 Public String intercept (actioninvocation arg0) throws exception {26 // get the session scope 27 Map <string, Object> sessionmap = actioncontext. getcontext (). getsession (); 28 // obtain the login user information from the session scope 29 us Er user = (User) sessionmap. get ("user"); 30 31 // get the name of the Request Method in the HTTP request address 32 string methodname = arg0.getproxy (). getmethod (); 33 // get the action name 34 string actionname = arg0.getproxy () in the HTTP request address (). getactionname (); 35 36 // determine whether the user is empty. If it is not empty, it indicates logon. If it is empty, it indicates that no logon is performed. 37 If (user! = NULL) {38 // login, not blocking 39 systems. out. println ("executed upon request"); 40 arg0.invoke (); 41 system. out. println ("response time execution"); 42 43 return NULL; 44} else {45 // if it is null, but a login request is in progress, 46 If (methodname. equals ("logincheck") {47 system. out. println ("executed upon request"); 48 arg0.invoke (); 49 system. out. println ("response time execution"); 50 return NULL; 51} 52 53 sessionmap. put ("backmsg", "Sorry, please log on first and then perform the operation"); 54 return "login"; // return to the logon page 55} 56} 57 58 59}
View code

2. Configure the interceptor in struts. XML to implement the interceptor.

1 <? XML version = "1.0" encoding = "UTF-8"?> 2 <! Doctype struts public 3 "-// Apache Software Foundation // DTD struts configuration 2.3 // en" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 <! -- If the request address is actionname! Methodname, the configuration needs to be set, otherwise the access address is incorrect --> 8 <constant name = "struts. Enable. dynamicmethodinvocation" value = "true"/> 9 10 <! -- Development Mode --> 11 <constant name = "struts. devmode" value = "true"/> 12 13 <! -- Encoding format filtering --> 14 <constant name = "struts. i18n. encoding "value =" UTF-8 "> </constant> 15 16 17 18 <package name =" default "namespace ="/"extends =" struts-Default "> 19 20 <! -- Interceptor configuration includes interceptor + interceptor Stack --> 21 <interceptors> 22 <interceptor name = "logininterceptor" class = "com. bjsxt. shangxiaofei. interceptor. logininterceptor "> </interceptor> 23 <interceptor name =" secondinterceptor "class =" com. bjsxt. shangxiaofei. interceptor. secondinterceptor "> </interceptor> 24 <! -- If you want the custom Interceptor to take effect, you must configure the interceptor stack, add the default interceptor Stack --> 25 <Interceptor-stack name = "myinterceptorstack"> 26 <Interceptor-ref name = "defaultstack"> </Interceptor-ref> 27 <interceptor -Ref name = "logininterceptor"> </Interceptor-ref> 28 <Interceptor-ref name = "secondinterceptor"> </Interceptor-ref> 29 </Interceptor-stack> 30 </interceptors> 31 <! -- Apply the custom interceptor stack to the project, all requests on the project go through the interceptor Stack --> 32 <default-interceptor-ref name = "myinterceptorstack"> </default-interceptor-ref> 33 34 35 <! -- Actionname! Methodname Request Method Configuration --> 36 <action name = "useraction" class = "com. bjsxt. shangxiaofei. action. useraction "> 37 <result name =" success ">/page/success. JSP </result> 38 </Action> 39 40 41 </package> 42 </struts>
View code

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.