Example:
Action class: testaction. Java
Package action; import COM. opensymphony. xwork2.actionsupport; public class testaction extends actionsupport {private string name; Public String getname () {return name;} public void setname (string name) {This. name = Name ;}@ overridepublic string execute () throws exception {// todo auto-generated method stubsystem. out. println ("execute metod"); system. out. println (name); Return super.exe cute (); // return success ;}}
Custom interceptor 1: testinterceptor1.java
Package interceptor; import COM. opensymphony. xwork2.actioninvocation; import COM. opensymphony. xwork2.interceptor. abstractinterceptor; public class testinterceptor1 extends actinterceptor {@ overridepublic string intercept (actioninvocation Invocation) throws exception {system. out. println ("interceptor1 intercepted before action"); string result = invocation. invoke (); system. out. println ("interceptor1 intercepaction"); return result ;}}
Custom interceptor 2: testinterceptor2.java
Package interceptor; import COM. opensymphony. xwork2.actioninvocation; import COM. opensymphony. xwork2.interceptor. abstractinterceptor; public class testinterceptor2 extends actinterceptor {@ overridepublic string intercept (actioninvocation Invocation) throws exception {system. out. println ("interceptor2 intercepted before action"); string result = invocation. invoke (); system. out. println ("interceptor2 intercepaction"); return result ;}}
Struts. xml
<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype struts public "-// Apache Software Foundation // DTD struts configuration 2.0 // en" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name = "struts. devmode "value =" true "/> <package name =" default "extends =" struts-Default "> <interceptors> <! -- Configure the custom interceptor --> <interceptor name = "interceptor1" class = "interceptor. testinterceptor1 "/> <interceptor name =" interceptor2 "class =" interceptor. testinterceptor2 "/> </interceptors> <action name =" test "class =" action. testaction "> <result>/welcome. JSP </result> <Interceptor-ref name = "defaultstack"/> <Interceptor-ref name = "interceptor1"/> <Interceptor-ref name = "interceptor2"/> </ action> </package> </struts>
Successful processing result welcome. jsp file:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
Default homepage: index. jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
Deployment file web. xml
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
Test:
Enter:
Http: // localhost: 8080/projectname/test? Name = myname
Background console output:
Interceptor1 interceptor2 interceptor2 intercepaction before execute metodwninterceptor2 intercepaction after interceptor1 intercepaction
Conclusion: The preceding example shows that the interceptor of struts2 intercepts the action method before and after it is executed. Before the action method is executed, configure the interception first, and then configure the interception later. After the action method is executed, configure the interception first, and then configure the interception later. The interception mechanism is cleverly designed.
Official figure of struts interception method:
Please note that the actioninvocation parameter of the intercept (actioninvocation Invocation) in the interceptor class is automatically filled by struts2 and contains the reference of the intercepted action, you can call the invoke () method of this parameter to forward the Controller to the next interceptor, or to the execute () method of the action (refer to the test result in the example ).
Actioninvocation is the scheduler in struts2. In fact, the scheduling and execution of these codes are completed in the implementation class of actioninvocation. Here, I have extracted the invoke () method in defaultactioninvocation, it will show us everything.
With the source code of invoactionactioninvocation invoke:
/*** @ Throws configurationexception if no result can be found with the Returned Code */Public String invoke () throws exception {string profilekey = "INVOKE:"; try {utiltimerstack. push (profilekey); If (executed) {Throw new illegalstateexception ("action has already executed");} // call the interceptor code in the interceptor stack to execute if (interceptors. hasnext () {final interceptormapping interceptor = (interceptormapping) interc Eptors. next (); utiltimerstack. profile ("Interceptor:" + interceptor. getname (), new utiltimerstack. profilingblock <string> () {Public String doprofiling () throws exception {// use actioninvocation as a parameter and call the intercept method in Interceptor to execute resultcode = interceptor. getinterceptor (). intercept (defaactionactioninvocation. this); return NULL ;}}) ;}else {resultcode = invokeactiononly () ;}// this is needed because the re Sult will be executed, then control will return to the interceptor, which will // return abve and flow through again if (! Executed) {// execute preresultlistener if (preresultlisteners! = NULL) {for (iterator = preresultlisteners. iterator (); iterator. hasnext ();) {preresultlistener listener = (preresultlistener) iterator. next (); string _ profilekey = "preresultlistener:"; try {utiltimerstack. push (_ profilekey); listener. beforeresult (this, resultcode);} finally {utiltimerstack. pop (_ profilekey) ;}}// now execute the result, if we're supposed to // action and interceptor are finished, execute result if (proxy. getexecuteresult () {executeresult ();} executed = true;} return resultcode;} finally {utiltimerstack. pop (profilekey );}}