拦截器类
//继承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>