Struts. xml configuration:
<! -- Struts2 interceptor -->
<Package name = "struts2" extends = "json-default">
<Interceptors>
<! -- Custom interceptor -->
<Interceptor name = "myinterceptor"
Class = "com. rd. common. util. MyInterceptor">
</Interceptor>
<! -- Custom interceptor Stack -->
<Interceptor-stack name = "myDefaultStack">
<Interceptor-ref name = "defaultStack">
</Interceptor-ref>
<Interceptor-ref name = "myinterceptor">
</Interceptor-ref>
</Interceptor-stack>
</Interceptors>
<! -- Set the default interceptor for the custom interceptor Stack -->
<Default-interceptor-ref name = "myDefaultStack">
</Default-interceptor-ref>
<! -- Define global Result -->
<Global-results>
<! -- When the login view name is returned, go to the/login. jsp page -->
<Result name = "login_out">/show_login.jsp </result>
</Global-results>
</Package>
Note: After the interceptor in struts. xml is configured, if you want to use the interceptor, you can let that folder inherit the folder, that is, extends = "struts2.
Interceptor class definition:
Package mypackage;
Import java. util. Map;
Import com. opensymphony. xwork2.ActionContext;
Import com. opensymphony. xwork2.ActionInvocation;
Import com. opensymphony. xwork2.interceptor. Interceptor;
@ SuppressWarnings ("serial ")
Public class MyInterceptor implements Interceptor {
@ Override
Public void destroy (){
System. out. println ("------- the interceptor is destroyed -------");
}
@ Override
Public void init (){
System. out. println ("------ the interceptor is initialized ------");
}
@ SuppressWarnings ("rawtypes ")
@ Override
Public String intercept (ActionInvocation arg0) throws Exception {
ActionContext ctx = arg0.getInvocationContext ();
Map session = ctx. getSession ();
Integer userId = (Integer) session. get ("userid ");
If (userId = null ){
Ctx. put ("tip", "You are not logged on. Please log on again! ");
Return "login_out ";
}
Return arg0.invoke ();
}
}