質問

I define a custom interceptor used to log time, but it doesn't work. here is my custom interceptor code:

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;
}

}

I define a new xml file: ecs-default.xml(it is directly under src package)

<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>

and struts.xml file:

<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>

I debug the code, in com.opensymphony.xwork2.DefaultActionInvocation class, List<InterceptorMapping> interceptorList = new ArrayList<InterceptorMapping>(proxy.getConfig().getInterceptors());, proxy.getConfig().getInterceptors() returns the 18 interceptors of defaultStack defined in struts-default.xml, my ecsTimer interceptor is not in it. so why? Is something wrong in my configuration? Thanks.

役に立ちましたか?

解決

The DefaultActionInvocation is being created for every action execution. You just need to execute your action which is configured with your custom interceptor to see it in debug.

BTW there is already timer interceptor that does what you want.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top