標籤:
一、攔截器相關知識
1、Struts2架構剖析
Holly版本生活案例:
影視公司(拍電影) ActionMapper
傳媒公司(封裝明星) ActionMapping
明星 Action
經紀人 ActionProxy(代理對象)
小工所在單位 ActionInvocation
小工 Interceptor(攔截器)
遞迴==99歸一
2、struts2工作原理
3、攔截器工作原理
攔截器圍繞著Action和Result的執行而執行。攔截器的工作原理類似遞迴的九九歸一!
攔截器執行的三個階段,有條件的執行循環:
(1)做一些Action執行前的預先處理:攔截器可以準備、過濾、改變或者操作任何可以訪問的資料,包括Action。
(2)調用ActionInvocation的invoke()方法將控制交給後續攔截器或返回結果字串終止執行:如果攔截器決定請求的處理不應該繼續,可以不調用invoke()方法,而是直接返回一個控制字元串。通估這種方式,可以停止後續的執行,並且決定哪個結果呈現給用戶端。
(3)做一些Action執行後的處理:此時攔截器依然可以改變可以訪問的對象和資料,只是此時架構已經選擇了一個結果呈現給用戶端了。
二、自訂攔截器
1、建立如下項目結構
2、在src下的com.action包下建立MyTimerAction.java
1 package com.action; 2 3 import com.opensymphony.xwork2.Action; 4 5 /** 6 * 記錄執行時間的Action 7 * @author Holly老師 8 * 9 */10 public class MyTimerAction implements Action {11 12 public String execute() throws Exception {13 System.out.println("Action記錄執行時間");14 return SUCCESS;15 }16 17 }MyTimerAction.java
3、在src下的com.interceptor包下建立自訂攔截器類MyTimerInterceptor.java
1 package com.interceptor; 2 3 import org.apache.struts2.ServletActionContext; 4 5 import com.opensymphony.xwork2.ActionInvocation; 6 import com.opensymphony.xwork2.interceptor.AbstractInterceptor; 7 import com.opensymphony.xwork2.interceptor.Interceptor; 8 /** 9 * 自訂攔截器:10 * 記錄動作執行所花費的時間11 * @author pc12 *13 */14 public class MyTimerInterceptor implements Interceptor{15 16 public void destroy() {17 System.out.println("銷毀的方法....");18 19 }20 21 public void init() {22 System.out.println("初始化方法....");23 24 }25 26 27 /**28 * 攔截器執行的入門方法(小工的某個技能)29 * @param invocation 經紀人30 */31 public String intercept(ActionInvocation invocation) throws Exception {32 //1.執行Action之前的工作:擷取開始執行的時間33 long startTime=System.currentTimeMillis();34 System.out.println("執行Action之前的工作,開始時間"+startTime);35 36 //2.執行後續的攔截器或Action(接收經紀人的口令)37 String result=invocation.invoke();38 39 //3.執行Action之後的工作:計算並輸出執行的時間40 long endTime=System.currentTimeMillis();41 42 long execTime=endTime-startTime;43 44 System.out.println("執行Action後的,結束時間"+endTime);45 System.out.println("總共用時:"+execTime);46 47 //返回結果字串 (經紀人口令給下一個小工)48 return result;49 50 }51 52 53 }MyTimerInterceptor .java
4、在src下建立struts.xml檔案並配置攔截器
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 <!-- 中文亂碼處理 --> 5 <constant name="struts.i18n.encoding" value="UTF-8"/> 6 7 <package name="default" namespace="/" extends="struts-default"> 8 <!-- 配置所有攔截器的節點 --> 9 <interceptors>10 <interceptor name="myTimer" class="com.interceptor.MyTimerInterceptor"></interceptor>11 </interceptors> 12 13 <!-- 配置Action=明星 -->14 <action name="myTimer" class="com.action.MyTimerAction">15 <result>/index.jsp</result>16 <!-- 引用攔截器==小工 -->17 <interceptor-ref name="myTimer"></interceptor-ref>18 <interceptor-ref name="defaultStack"></interceptor-ref>19 </action> 20 </package>21 </struts>
struts.xml
5、編輯WebRoot下的WEB-INF下的web.xml檔案
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app id="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>10 <url-pattern>/*</url-pattern>11 </filter-mapping>12 13 <welcome-file-list>14 <welcome-file>index.jsp</welcome-file>15 </welcome-file-list>16 17 </web-app>
web.xml
6、運行項目
7、查看控制台的輸出
5、Struts2自訂攔截器