Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Struts and the fly likes why and when are interceptors invoked? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "why and when are interceptors invoked?" Watch "why and when are interceptors invoked?" New topic

why and when are interceptors invoked?

Rajiv Chelsea
Ranch Hand

Joined: Jun 15, 2010
Posts: 88
To quote from the book "Manning: Struts 2 in Action" about interceptors:

"The interceptors are
invoked both before and after the action, though we should note that they actually fire
after the result has executed. Interceptors don’t necessarily have to do something both
times they fire, but they do have the opportunity. Some interceptors only do work
before the action has been executed, and others only do work afterward."

what is meant by they are actually fire after the result? what do we mean by invoking an interceptor
and the firing of an interceptor? Also, if an interceptor is fired only after the result, how is it fired two time
as mentioned in the above paragraph?
Joe Ess

Joined: Oct 29, 2001
Posts: 8836

Wow. That's an awful explanation of interceptors. Let me try:
Interceptors get piled on the call stack. The pattern of an interceptor looks like so:

Let's say you wanted to hand your actions a database connection. In the "pre action" area, you 'd get a connection from the pool and set it somewhere your action can access it.
The call to invoke either calls the next interceptor or, at the bottom of the interceptor stack, your action. The return value from invoke is the return value from your action.
In the "post action" area, you'd return the connection to the pool.
Does that make any more sense?

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
David Newton

Joined: Sep 29, 2008
Posts: 12617

I agree--that's a pretty weird explanation, and misleading.

The only thing I'd add to Joe's explanation is that while the result *is* the result of invoking the action, it's also *after* the result has been rendered: personally, I find this a little counter-intuitive, so feel obligated to point it out in case it confuses people like it confuses me :)
Rajiv Chelsea
Ranch Hand

Joined: Jun 15, 2010
Posts: 88
Hi guys,

Can you provide the link to any good book/tutorial on Struts 2..
and can you please define the role of interceptors
Fidel Edwards
Ranch Hand

Joined: Mar 19, 2008
Posts: 231
Hi Rajiv,
I found this link very useful in understanding the Struts2 flow.
Struts 2 Flow

I am also new to Struts 2 but I read the flow from this website and Apache's site and tried to draw diagram in paper for better interpretation. It helped me lot. You can also google images for Struts 2 Interceptor Hoping will help you also.......

God Gave Me Nothing I Wanted, He Gave Me Everything I Needed.
David Newton

Joined: Sep 29, 2008
Posts: 12617

Rajiv Chelsea wrote:and can you please define the role of interceptors

They are where to put a Struts 2 application's cross-cutting concerns. See the existing interceptors for some idea of what kinds of things can go there.
jQuery in Action, 2nd edition
subject: why and when are interceptors invoked?
Similar Threads
Setting up interceptors to be invoked after a business method.
endless loop with interceptor
Doubt on struts2 Architechture.
Delete file when the file downloaded.
Need Help about Interceptors in Struts2