views:

18

answers:

1

I'm implementing some stuff in my Struts-based application using interceptors, and I'm getting confused about how their lifecycle works. According to the Struts docs ("Interceptors", "Writing interceptors" and "Big picture"), it should work something like this:

FirstInterceptor
  NextInterceptor
    LastInterceptor
      Action
      Result
    LastInterceptor
  NextInterceptor
FirstInterceptor

which makes sense, but I'm stumbling on how to distinguish an interceptor call executing before the action from one executing after the result is rendered (I'm skipping PreResultListeners here). If I fire up a debugger, I get two calls to intercept() and can't find anything too obvious on the ActionInvocation I'm being passed. (Update: This part was a major confusion on my end, and I was able to answer my question below once I got it)

The "Big picture" page talks somewhat confusingly of a "before" and "after" "clause" that are called, but I don't know what to make of it:

[...]

This includes invoking any Interceptors (the before clause) in advance of invoking the Action itself.

[...]

Interceptors are executed again (in reverse order, calling the after clause).

[...]

(Update: These two sentences are still bad, though)

A: 

There's no two calls to the interceptor:

public class MyInterceptor implements Interceptor {

    public String intercept(ActionInvocation invocation) {
        /*
        This is before Action.execute(),
        and before all interceptors down the stack
        */

        String code = invocation.invoke();

        /*
        This is after Action.execute(),
        the result rendering and all
        interceptors down the stack,
        but before the interceptors
        higher up in the stack.
        */

        return code;
    }

}

(Note that the "two calls to intercept" I was witnessing in the debugger were a result of a less obvious redirect that I failed to notice. This confused me a lot.)

Hanno Fietz