struts2的action執行過程類比

來源:互聯網
上載者:User

package xwork;

import java.io.Serializable;

/**
 * @author wangmingjie
 * @date 2008-9-26上午11:09:05
 */
public interface Interceptor extends Serializable {
    String intercept(ActionInvocation invocation) throws Exception;
}
==============================================

package xwork;
/**
 * @author wangmingjie
 * @date 2008-9-26上午11:11:56
 */
public class FirstInterceptor implements Interceptor {

 public String intercept(ActionInvocation invocation) throws Exception {
  String resultCode = null;
  System.out.println("before first Interceptor ");
  resultCode = invocation.invoke();
  System.out.println("after first Interceptor ");
  return resultCode;
 }

}
==============================================

package xwork;
/**
 * @author wangmingjie
 * @date 2008-9-26上午11:13:34
 */
public class SecondInterceptor implements Interceptor {

 public String intercept(ActionInvocation invocation) throws Exception {
  String resultCode = null;
  System.out.println("before second Interceptor ");
  resultCode = invocation.invoke();
  System.out.println("after second Interceptor ");
  return resultCode;
 }

}

==================核心調用過程============================

package xwork;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author wangmingjie
 * @date 2008-9-26上午11:10:00
 */
public class ActionInvocation {
 private boolean  executed = false;
 private String resultCode = null;
 private Iterator interceptors;
 private void init(){
        List<Interceptor> interceptorList = new ArrayList<Interceptor>();
        interceptorList.add(new FirstInterceptor());
        interceptorList.add(new SecondInterceptor());
        interceptors = interceptorList.iterator();
 }
 
 public String invoke() throws Exception {
  if(executed){
   throw new Exception("已經執行了");
  }
  if (interceptors.hasNext()) {//下面就是核心實現代碼
   resultCode = ((Interceptor)interceptors.next()).intercept(ActionInvocation.this);//注意這裡,將自己作為參數傳入,這是一種遞迴的調用方法。
  } else {
   resultCode = "success";//執行action的方法
   System.out.println("執行action");
  }
  
  if (!executed) {
   System.out.println("執行preResultListener");
   executed = true;
  }
  return null;
 }
 
 public static void main(String[] args) throws Exception{
  //再進行斷點跟蹤一下。
  ActionInvocation ai = new ActionInvocation();
  ai.init();
  ai.invoke();
 }
 
}

================執行結果如下==============================

before first Interceptor
before second Interceptor
執行action
執行preResultListener
after second Interceptor
after first Interceptor
=========================================================

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.