First, Interceptor related knowledge
1, STRUTS2 framework Analysis
Holly Version Life case:
Film and television Company (filming) Actionmapper
Media company (packaging star) actionmapping
Star Action
Broker Actionproxy (Agent object)
The unit is located actioninvocation
Handyman Interceptor (Interceptor)
Recursive ==99
2. Struts2 Working principle
3. How interceptors work
Interceptors are executed around the execution of action and result. The interceptor works like a recursive Jiujiuguiyi!
Three stages of the interceptor execution with a conditional execution cycle:
(1) Do some preprocessing before action: The Interceptor can prepare, filter, change, or manipulate any data that can be accessed, including the action.
(2) Call the Invoke () method of the actioninvocation to give control to the subsequent interceptor or return the result string to terminate execution: if the interceptor decides that the processing of the request should not continue, you can return a control string instead of invoking the Invoke () method. This way, you can stop subsequent executions and decide which results are presented to the client.
(3) Do some action after the processing: The interceptor can still change the object and data can be accessed, but the framework has selected a result presented to the client.
Ii. Custom Interceptors
1. Create the following project structure
2. Create Mytimeraction.java under the Com.action package under SRC
1 Packagecom.action;2 3 Importcom.opensymphony.xwork2.Action;4 5 /**6 * Action to record execution time7 * @authorHolly Teacher8 *9 */Ten Public classMytimeractionImplementsAction { One A PublicString Execute ()throwsException { -System.out.println ("Action Record Execution Time"); - returnSUCCESS; the } - -}
Mytimeraction.java
3. Create a custom interceptor class under the Com.interceptor package under SRC Mytimerinterceptor.java
1 PackageCom.interceptor;2 3 ImportOrg.apache.struts2.ServletActionContext;4 5 Importcom.opensymphony.xwork2.ActionInvocation;6 ImportCom.opensymphony.xwork2.interceptor.AbstractInterceptor;7 ImportCom.opensymphony.xwork2.interceptor.Interceptor;8 /**9 * Custom Interceptors:Ten * Record the time it takes to execute the action One * @authorPC A * - */ - Public classMytimerinterceptorImplementsinterceptor{ the - Public voiddestroy () { -System.out.println ("Method of Destruction ....")); - + } - + Public voidinit () { ASystem.out.println ("Initialize Method ....")); at - } - - - /** - * How to get started with interceptors (a skill for a handyman) in * @paramInvocation Brokers - */ to PublicString Intercept (actioninvocation invocation)throwsException { + //1. Work before executing action: Gets the time to start execution - LongStarttime=System.currenttimemillis (); theSystem.out.println ("Work before executing action, start time" +startTime); * $ //2. Execute the subsequent interceptor or action (receive broker's password)Panax NotoginsengString result=Invocation.invoke (); - the //3. Work after action: calculate and output the execution time + LongEndtime=System.currenttimemillis (); A the Longexectime=endtime-StartTime; + -System.out.println ("End time after executing action" +endTime); $SYSTEM.OUT.PRINTLN ("Total time:" +exectime); $ - //returns the result string (broker password to next handyman) - returnresult; the - }Wuyi the -}
Mytimerinterceptor. Java
4. Create the Struts.xml file under SRC and configure the Interceptor
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <!DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.1//en" "Struts-2.1.dtd ">3 <Struts>4 <!--Chinese garbled processing -5 <constantname= "Struts.i18n.encoding"value= "UTF-8"/>6 7 < Packagename= "Default"namespace="/"extends= "Struts-default">8 <!--Configure nodes for all interceptors -9 <Interceptors>Ten <Interceptorname= "MyTimer"class= "Com.interceptor.MyTimerInterceptor"></Interceptor> One </Interceptors> A - <!--Configure action= stars - - <Actionname= "MyTimer"class= "Com.action.MyTimerAction"> the <result>/index.jsp</result> - <!--reference interceptors = = Handyman - - <Interceptor-refname= "MyTimer"></Interceptor-ref> - <Interceptor-refname= "Defaultstack"></Interceptor-ref> + </Action> - </ Package> + </Struts>
Struts.xml
5. Edit the Web. xml file under Web-inf under Webroot
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Web-appID= "Webapp_9"version= "2.4"xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">3 <Filter>4 <Filter-name>Struts2</Filter-name>5 <Filter-class>Org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</Filter-class>6 </Filter>7 8 <filter-mapping>9 <Filter-name>Struts2</Filter-name>Ten <Url-pattern>/*</Url-pattern> One </filter-mapping> A - <welcome-file-list> - <Welcome-file>index.jsp</Welcome-file> the </welcome-file-list> - - </Web-app>
Web. XML
6. Running the project
7. View the console output
5. STRUTS2 Custom Interceptor