Using Interceptors in Struts2
January 24th, 2008 | by Admin |Interceptors are classes which implement com.opensymphony.xwork2.interceptor.Interceptor interface. Interceptors are configured in struts.xml. Interceptors are used for pre-processing and post-processing on Action invocations. Multiple interceptors can be applied to an action request.
Interceptor stack is a group of interceptors which can be referenced together. So instead of configuring a number of interceptors every time, we can just configure an interceptor stack with all the required interceptors in it.
Struts2 configures a default interceptor stack which handles all the generic functionalities required by action classes. These include Exception Interceptor, Validation Interceptor etc.
To demonstrate the use of interceptors, let us build an interceptor named “actiontimer” which will print the time required by an action request in milliseconds on the console. To ensure that time taken by default interceptors are not included, we will configure actiontimer as the last interceptor after default interceptor stack.
ActionTimer.java
struts.xml
interceptordemo.jsp
By panji on Feb 12, 2008
Is this a complete example ?
By Andres on Feb 13, 2008
Great article!, just what I needed.
By kibo on Feb 19, 2008
Hi
thank you very much. This article help me, when I need it. All the time I found how interceptor dispose to before part and after part. I did not find it anywhere.
By Siriwardana Loku Kaluge on Mar 29, 2008
This is very helpful. Please keep this up. Thank you so much.
By Juan Pablo on Apr 10, 2008
I’ve problems with that demo, I mean, I coded all as you posted, but the effect in my project is nos good.
When I navigate my site, if I enable the ActionTimer, the actions not go to the next, is re-calling it selft.
An example:
- From Welcome.jsp I excecute a Login action
- Login action shows a form to enter user and pass
- When I click at “login”, it should redirect to menu.jsp and shows the time in console (for the interceptor) but appear login again as infinite loop.
Well, I think that the tutorial is great, I’ve had this tutorials to learn Struts 2 and the results are very good.
Sorry for my English.
Juan Pablo.