直接上图
struts.xml核心配置文件中interceptor的注册和调用:
<struts>
<package name="default" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="myTimer" class="com.testaction.TimerIntercept"/>
<interceptor name="testInterceptor" class="com.testaction.TestInterceptor"/>
</interceptors>
<action name="timer" class="com.testaction.TimerAction">
<result>/success.jsp</result>
<interceptor-ref name="myTimer"/>
<interceptor-ref name="testInterceptor"/>
</action>
</package>
<constant name="struts.action.extension" value="action,do,html"></constant>
</struts>
Action类:
package com.testaction;
import com.opensymphony.xwork2.ActionSupport;
public class TimerAction extends ActionSupport{
@Override
public String execute() throws Exception {
for(int i=0;i<10;i++){
System.out.println("I LOVE YOU");
}
return SUCCESS;
}
}
自定义的两个拦截器类:
1、
package com.testaction;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class TimerIntercept extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation invocation) throws Exception {
long start=System.currentTimeMillis();
System.out.println("000000000000000");
System.out.println("11111111111111");
String result=invocation.invoke();
long end=System.currentTimeMillis();
System.out.println("222222222222222");
System.out.println("3333333333333333");
System.out.println("执行Action所用时间:"+(end-start)+"ms");
return result;
}
}
2、
package com.testaction;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class TestInterceptor extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("/");
System.out.println("/");
System.out.println("############################");
System.out.println("############################");
String result=invocation.invoke();
System.out.println("*****************************");
System.out.println("*****************************");
System.out.println("=================================");
System.out.println("================================");
return result;
}
}
控制台打印输出结果:
000000000000000
11111111111111
/
/
############################
############################
I LOVE YOU
I LOVE YOU
I LOVE YOU
I LOVE YOU
I LOVE YOU
I LOVE YOU
I LOVE YOU
I LOVE YOU
I LOVE YOU
I LOVE YOU
*****************************
*****************************
=================================
================================
222222222222222
3333333333333333
执行Action所用时间:495ms