Struts2 built-in custom interceptor for JSP development, jspstruts2
Struts2 built-in custom interceptor for JSP development
The custom interceptor of Struts2 is mainly used to parse request parameters, assign request parameters to Action attributes, perform data verification, file upload, and other operations. To extend the Struts2 function, we only need to provide the corresponding interceptor and configure it to the Struts2 container. When we do not need to use it, you only need to cancel the configuration in the Struts2 container.
1> the configuration interceptor mainly uses four configuration elements:
: Used to configure a custom interceptor
: Used to reference the interceptor or interceptor Stack
: Used to configure a custom interceptor stack. An Interceptor stack consists of multiple interceptors and can contain other interceptor stacks.
: Has configuration parameters
<! -- Configure the custom interceptor --> <interceptor name = "interceptor name" class = "implementation class"> <param name> .. </param>... </interceptor> <! -- Configure the interceptor stack, used to reference multiple interceptors at a time --> <interceptor-stack name = "interceptor stack name"> <interceptor-ref name = "interceptor 1"/> <interceptor-ref name =" interceptor 2 "> <param name> .. </param>... </interceptor-ref> <! -- You can also reference the interceptor stack --> <interceptor-ref name = "interceptor stack name"/> </interceptor-stack>
2> after the interceptor is configured, you can use the interceptor in the element. When using the custom interceptor, you must also use the default system interceptor defastack stack.
<Package...> <! -- Configure the default interceptor for all actions in the package -- "<! -- If Action is configured with its own interceptor, the default interceptor will not be configured under this Action --> <default-interceptor-ref name = "defaultStack"/> <action...> <! -- Configure your interceptor --> <interceptor-ref name = "myInterceptor"/> <! -- Because your interceptor has been configured, the default interceptor configuration of the package will not work, therefore, you must display and configure the default interceptor of the defaultStack System --> <interceptor-ref name = "defaultStack"/> </action> </package>
3> Custom Interceptor: Struts2 provides the Native Interface of Interceptor:
Public interface Interceptor extends Serializable {// callback method of the initialization Interceptor void init (); // Processing Method for blocking String intercept (ActionInvocation invocation) throws Exception; // destroy the callback method void destroy ();}
Therefore, the User-Defined interceptor class needs to implement this interface and provide implementation of all abstract methods in this interface. However, Struts2 also provides a direct implementation class for this interface: AbstractInterceptor, which provides the implementation of the init () and destroy () methods. We only need to override the intercept () Interception Method:
Public class simpleInterceptor extends actinterceptor {private String username; private String password; // omit setter and getter methods @ Override public String intercept (ActionInvocation invocation) throws Exception {// get the intercepted Action instance LoginAction action = (LoginAction) invocation. getAction (); System. out. println ("start interception:" + new Date (); // gets the time for starting to execute the Action processing method long start = System. currentTimeMillis (); // Method for blocking the callback execution Action String result = invocation. invoke (); System. out. println ("end execution:" + new Date (); // get the time after the Action processing method is executed Long end = System. currentTimeMillis (); System. out. println ("execution time:" + (end-start) + "ms"); return result ;}}
The interceptor defined in the AbstractInterceptor class provided above intercepts all the processing methods in the Action. If you only want to intercept the specified method, or you want to specify which methods to intercept and which methods not to intercept, you can use the MethodFilterInterceptor class provided by Struts2. This class is a subclass of the AbstractInterceptor class and provides the following three main methods:
1> String doIntercept (ActionInvocation invocation) throws Exception: Same as intercept () method
2> void setIncludeMethods (String includeMethods): sets the method name to be blocked.
3> void setExcludeMethods (String excludeMethods): Set the method name that does not need to be blocked
Two setter methods can specify the attribute value through the param element when configuring the interceptor.
<interceptor name="simpleIntercept2" class="com.chengxi.SimpleInterceptor2"> <param name="includeMethods">login,execute</param> <param name="excludeMethods">regiset</param></interceptor>
4> execution sequence of the Interceptor: if multiple interceptor acts on an Action at the same time, follow the following rule: the interceptor defined first takes effect before the Action control method is executed; after the Action control method is executed, the interceptor is defined first and then executed; that is, in invocation. invoke () method.
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!