我定义了一个用于记录时间的自定义拦截器,但它不起作用。这是我的自定义拦截器代码:
public class TimeConsumedInterceptor extends AbstractInterceptor
{
/*
* {@inheritDoc}
*/
@Override
public String intercept(ActionInvocation invocation) throws Exception
{
long start = System.currentTimeMillis();
String result = invocation.invoke();
long end = System.currentTimeMillis();
System.out.println("time consumed: " + (end - start));
return result;
}
}
我定义了一个新的xml文件:ecs-default.xml(它直接在src
包下)
<struts>
<include file="struts-default.xml"></include>
<!-- ecs-default package is abstract -->
<package name="ecs-default" extends="struts-default" abstract="true">
<interceptors>
<interceptor name="ecsTimer" class="com.nader.interceptor.TimeConsumedInterceptor"></interceptor>
<interceptor-stack name="ecsStack">
<interceptor-ref name="ecsTimer"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- default stack used for ecs-default package -->
<default-interceptor-ref name="ecsStack"></default-interceptor-ref>
<global-results>
<result name="error">error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error" />
</global-exception-mappings>
</package>
</struts>
和 struts.xml 文件:
<struts>
<include file="ecs-default.xml"></include>
<!-- define two empty package, / and /manager, extends ecs-default package -->
<package name="default" namespace="/" extends="ecs-default">
</package>
<package name="manager" namespace="/manager" extends="ecs-default">
</package>
</struts>
com.opensymphony.xwork2.DefaultActionInvocation
我在类中调试代码,List<InterceptorMapping> interceptorList = new ArrayList<InterceptorMapping>(proxy.getConfig().getInterceptors());
返回proxy.getConfig().getInterceptors()
struts-default.xml中定义的defaultStack的18个拦截器,我的ecsTimer拦截器不在里面。所以为什么?我的配置有问题吗?谢谢。