攔截器類
//繼承AbstractInterceptor類
public class MyTimerInterceptor extends AbstractInterceptor{
//重寫intercept方法
publicString intercept(ActionInvocation invocation)
throwsException {
//預處理工作
long startTime = System.currentTimeMillis();
//執行後續攔截器或Action
String result = invocation.invoke();
//後續處理工作
longexecTime= System.currentTimeMillis() - startTime;
System.out.println("The interval time is "+execTime+"ms");
//返回結果字符串
return result;
}
}
配置文件中
<package name="default"namespace="/" extends="struts-default">
<interceptors>
//定義攔截器
<interceptorname="myTimer"
class="com.pb.interceptor.MyTimerInterceptor">
</interceptor>
</interceptors>
<actionname="action" class="com.pb.action.MyTimerAction">
<result>/index.jsp</result>
//引用攔截器
<interceptor-refname="myTimer"></interceptor-ref>
<interceptor-refname="defaultStack"></interceptor-ref>
</action>
</package>
配置多個攔截器
<package name="packName"extends="struts-default" namespace="/manage">
<interceptors>
<!--定義攔截器 -->
<interceptor name="interceptorName" class="interceptorClass" />
<!--定義攔截器棧 -->
<interceptor-stack name="interceptorStackName">
<!--指定引用的攔截器-->
<interceptor-ref name="interceptorName|interceptorStackName" />
</interceptor-stack>
</interceptors>
<!--定義默認的攔截器引用-->
<default-interceptor-refname="interceptorName|interceptorStackName" />
<actionname="actionName" class="actionClass">
<!—爲Action指定攔截器引用-->
<interceptor-ref name="interceptorName|interceptorStackName" />
<!--省略其他配置-->
</action>
</package>