? Struts2 interceptors can intercept a number of requests, such as users do not log in to access a page, such as the user does not have access to a page, but send access requests, these are to be intercepted, based on these requirements, I studied the Struts2 interceptor.
To use interceptors, you first need to configure them in Struts.xml, with the following configuration files:
/ web-inf/index/login.jsp mresult
It is worth noting that the name of the defined interceptor stack and interceptor-stack must be consistent.
Once configured, we need to write a interceptor class to handle requests for user access (allowing access or not, etc.), which is Logininterceptor.java in the class attribute of <interceptor> above:
Logininterceptor { string intercept (actioninvocation actioninvocation) Exception { String name = Actioninvocation.getinvocationcontext (). GetName (); (name.equals ()) { actioninvocation.invoke (); } { map session = actioninvocation.getinvocationcontext (). GetSession (); (session == ) { ServerCommonString.; } { string userid = (String) session.get (); String userPwd = (String) session.get (); String userType = (String) session.get (); (userid == | | userpwd == | | userType == ) { ServerCommonString.; } { actioninvocation.invoke (); } } } }}
It is important to note that this class inherits the Abstractinterceptor class and then overrides the Interceptor () method.
That's about it, that's pretty simple!
Configuration of the Struts2 interceptor