From: The path to growth http://xugw.blog.sohu.com/124311370.html
Two steps for Interceptor
1. Write your own interceptor public class privilegecheckinterceptor extends Abstractinterceptor {// Implement the interceptor Interface Public String intercept (actioninvocation Invocation) throws exception { System. Out. println ("privilegecheckinterceptor "); Return invocation. Invoke () ;}2. Configure the global Interceptor (struts. XML) <struts> <Package name = "default" extends = "struts-Default"> <Interceptors> <Interceptor name = "myinterceptor" class = "test. Interceptor. privilegecheckinterceptor"/> <Interceptor-stack name = "mystack"> <Interceptor-ref name = "myinterceptor"/> <Interceptor-ref name = "defaultstack"/> </Interceptor-stack> </Interceptors> <Default-interceptor-ref name = "mystack"/> <Action name = "users1" Class = "test. Action. usermanagementaction"> <Result name = "input">/index. jsp </result> <Result name = "success">/success. jsp </result> </Action> </Package> </struts> after this configuration, all actions defined in the package will be intercepted by privilegecheckinterceptor annotation. You can also configure the interceptor, however, the global interceptor still needs struts. xml configuration Note: The interception configuration should be placed before the action
Problem: Convention and interceptor (global) cooperation issues. Convention is not configured, And the interceptor must be defined in struts. xml,
How do I specify a global interceptor for all actions?
(This is a good question. I have been looking for a long time to find this answer) Solution: with the package inheritance relationship of struts2, The parentpackage interceptor is inherited by childpackage. Define the interceptor in struts. xml and add struts. Convention. Default. Parent. Package = default to struts. properties. Note that this package name ( Default ) The package name must be consistent with that in struts. xml. <package name =" Default "Extends =" struts-Default "> you can also use @ parentpackage (" Default ") Method to specify the parent package name problem: how to obtain the intercepted method name to solve: Use the interceptor object Actioninvocation Actionproxy proxy = actioninvocation. getproxy (); proxy. getmethod ();