国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

覆蓋DispatchAction中的分發方法

2019-11-18 11:58:23
字體:
來源:轉載
供稿:網友

  背景:
  
  在使用struts時我們經常會用到DispatchAction.有了這個類,我們不需要針對每一個Action都要寫一個特定的類,而是可以把一些相關的方法放到一個類中.
  
  DispatchActon中使用了reflection來根據你傳入的method參數的值來獲取相應的參數來處理你的請求.正如他的方法 -- 他根據你傳入的請求參數,用不同的方法來處理你的請求.
  
  但是,通常我們還會碰到另一種情況,假如我想在之行實際的action處理方法之前或者之后再去做一些別的事情而又不想修改我們實際存在的action類呢?這里我將介紹一種方法.
  
  只要看看struts中DispatchAction(以下簡寫做DA)的源文件你就會發現,它有一個dispatchMethod方法,接受5個參數.其中4個就是我們通常的struts action里的(mapping,request,response,form),還有一個參數就是指定方法的參數的名字.
  
  PRotected ActionForward dispatchMethod(ActionMapping mapping,
  ActionForm form, HttpServletRequest request,
  HttpServletResponse response, String name) throws Exception
  
  其實現方法如下:
  
  // Make sure we have a valid method name to call.
  // This may be null if the user hacks the query string.
  if (name == null) {
  return this.unspecified(mapping, form, request, response);
  }
  
  // Identify the method object to be dispatched to
  Method method = null;
  try {
  method = getMethod(name);
  
  } catch (NoSUChMethodException e) {
  String message = messages.getMessage("dispatch.method", mapping
  .getPath(), name);
  log.error(message, e);
  throw e;
  }
  
  ActionForward forward = null;
  try {
  Object args[] = { mapping, form, request, response };
  
  //非凡的url不進行處理(add)
  String actionUrl = request.getServletPath(); // "/rolesign.do"
  boolean exception = isException(actionUrl);
  if(exception) {
  logger.debug("requestUrl :"+actionUrl+",no pre and post process");
  }
  // 預處理(add)
  if (!disabled && !exception) {
  logger.debug("preProcess begin");
  preProcess(request);
  logger.debug("preProcess end");
  }
  
  forward = (ActionForward) method.invoke(this, args);
  
  // 后處理(add)
  if (!disabled && !exception ){
  logger.debug("postProcess begin");
  postProcess(request);
  logger.debug("postProcess end");
  }
  } catch (ClassCastException e) {
  String message = messages.getMessage("dispatch.return", mapping
  .getPath(), name);
  log.error(message, e);
  throw e;
  
  } catch (IllegalaccessException e) {
  String message = messages.getMessage("dispatch.error", mapping
  .getPath(), name);
  log.error(message, e);
  throw e;
  
  } catch (InvocationTargetException e) {
  // Rethrow the target exception if possible so that the
  // exception handling machinery can deal with it
  Throwable t = e.getTargetException();
  if (t instanceof Exception) {
  throw ((Exception) t);
  } else {
  String message = messages.getMessage("dispatch.error", mapping
  .getPath(), name);
  log.error(message, e);
  throw new ServletException(t);
  }
  }
  
  // Return the returned ActionForward instance
  return (forward);
  
  大部分代碼還是從DA的實現方法中copy過來,但是我在這里加入了3個地方.分別是:
  
  1.對請求url的識別,確定是否使用預/后處理
  
  2.預處理方法調用
  
  3.后處理方法調用
  
  當然你要自己寫預處理方法和后處理方法.這里你可以傳任意你想要的參數給request,然后通過reflection來動態調用任意的java方法,比如你可以在頁面設置一個preFunction參數,它的值是classname.methodname.然后你可以在preProcess的實現中通過reflection來查找這個方法并執行.
  
  假如你想讓項目中所有的action都有這種特性,則只要讓根Action繼續DA并覆蓋它的dispatchMethod方法即可.
  
  這是在實際項目中用到的一個特性,為了盡可能少的修改原有的代碼使用了這種折中的做法.使用reflection時建議不要執行太過復雜的方法,可能會使你的action響應慢的.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宝山区| 邓州市| 汉沽区| 柳河县| 奈曼旗| 安西县| 凤翔县| 大新县| 绥化市| 昂仁县| 中阳县| 齐齐哈尔市| 宽城| 闻喜县| 吴忠市| 榆社县| 桐梓县| 平江县| 安阳县| 莱西市| 棋牌| 蓬溪县| 濉溪县| 合阳县| 易门县| 乡宁县| 九江县| 乐业县| 河北区| 石林| 县级市| 吉林省| 巴青县| 旬阳县| 米泉市| 西宁市| 奈曼旗| 汤原县| 长沙市| 昌都县| 临澧县|