I picked two struts2 interceptor configuration methods from the official website and recorded them myself:
Method 1: configure a single interceptor
<package name="default" namespace="/system/login" extends="struts-default"> <interceptors> <interceptor name="timer" class="com.zyujie.common.TimerInterceptor"/> <interceptor name="logger" class="com.zyujie.common.LoggerInterceptor"/> </interceptors> <action name="login" class="userLogin" method="login"> <interceptor-ref name="timer"/> <interceptor-ref name="logger"/> <result name="input">login.jsp</result> <result name="success" type="redirectAction">/secure/home</result> </action></package>
Method 2: configure the interceptor through the interception Stack
<package name="default" namespace="/system/login" extends="struts-default"> <interceptors> <interceptor name="timer" class="com.zyujie.common.TimerInterceptor"/> <interceptor name="logger" class="com.zyujie.common.LoggerInterceptor"/> <interceptor-stack name="myStack"> <interceptor-ref name="timer"/> <interceptor-ref name="logger"/> </interceptor-stack> </interceptors> <action name="login" class="userLogin" method="login"> <interceptor-ref name="myStack"/> <result name="input">login.jsp</result> <result name="success" type="redirectAction">/secure/home</result> </action></package>
The third method is to reference struts2 as our default interceptor.
<Package name = "Upload" namespace = "/system/upload" extends = "struts-Default"> <action name = "fileuploadother" class = "myfileuploadaction" method = "fileuploadother"> <! -- Dynamically set the savepath attribute value to the path on the server --> <Param name = "savepath"> D: \ webapp \ apache-Tomcat-6.0.10 \ webapps \ struts2upload \ upfiles </param> <Param name = "fileopfilename"> abcde.xls </param> <result name = "success" type = "Redirect">/OK. JSP </result> <result name = "input" type = "Redirect">/index. JSP </result> <! -- Implement the default interceptor function of Struts. Therefore, a defaultstack should be added to the backend to interceptor. --> <Interceptor-ref name = "fileupload"> <! -- File filtering --> <Param name = "allowedtypes"> image/BMP, image/PNG, image/GIF, image/JPEG, image/jpg </param> <! -- File size, in bytes --> <Param name = "maximumsize"> 2097152 </param> </Interceptor-ref> <! -- The default interceptor must be placed after fileupload; otherwise, it is invalid --> <Interceptor-ref name = "defaultstack"/> </Action> </package>
The following are two interception classes written by myself:
Public class loggerinterceptor extends actinterceptor {@ overridepublic string intercept (actioninvocation Invocation) throws exception {map = invocation. getinvocationcontext (). getsession (); If (map. get ("userid") = NULL) {/** user not logged on **/system. out. println ("No session, log on again. ");} Else {system. Out. println (" the session already exists. You do not need to log on again. ") ;}Return invocation. Invoke ();}}
Public class timerinterceptor extends actinterceptor {@ overridepublic string intercept (actioninvocation Invocation) throws exception {map = invocation. getinvocationcontext (). getsession (); If (map. get ("userid") = NULL) {/** user not logged on **/return action. input;} else {return invocation. invoke ();}}}