欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
覆蓋DispatchAction中的分發(fā)方法
背景:
  
  在使用struts時(shí)我們經(jīng)常會(huì )用到DispatchAction.有了這個(gè)類(lèi),我們不需要針對每一個(gè)Action都要寫(xiě)一個(gè)特定的類(lèi),而是可以把一些相關(guān)的方法放到一個(gè)類(lèi)中.
  
  DispatchActon中使用了reflection來(lái)根據你傳入的method參數的值來(lái)獲取相應的參數來(lái)處理你的請求.正如他的方法 -- 他根據你傳入的請求參數,用不同的方法來(lái)處理你的請求.
  
  但是,通常我們還會(huì )遇到另一種情況,如果我想在之行實(shí)際的action處理方法之前或者之后再去做一些別的事情而又不想修改我們實(shí)際存在的action類(lèi)呢?這里我將介紹一種方法.
  
  只要看看struts中DispatchAction(以下簡(jiǎn)寫(xiě)做DA)的源文件你就會(huì )發(fā)現,它有一個(gè)dispatchMethod方法,接受5個(gè)參數.其中4個(gè)就是我們通常的struts action里的(mapping,request,response,form),還有一個(gè)參數就是指定方法的參數的名字.
  
  protected ActionForward dispatchMethod(ActionMapping mapping,
  ActionForm form, HttpServletRequest request,
  HttpServletResponse response, String name) throws Exception
  
  其實(shí)現方法如下:
  
  // 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不進(jìn)行處理(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的實(shí)現方法中copy過(guò)來(lái),但是我在這里加入了3個(gè)地方.分別是:
  
  1.對請求url的識別,確定是否使用預/后處理
  
  2.預處理方法調用
  
  3.后處理方法調用
  
  當然你要自己寫(xiě)預處理方法和后處理方法.這里你可以傳任意你想要的參數給request,然后通過(guò)reflection來(lái)動(dòng)態(tài)調用任意的java方法,比如你可以在頁(yè)面設置一個(gè)preFunction參數,它的值是classname.methodname.然后你可以在preProcess的實(shí)現中通過(guò)reflection來(lái)查找這個(gè)方法并執行.
  
  如果你想讓項目中所有的action都有這種特性,則只要讓根Action繼承DA并覆蓋它的dispatchMethod方法即可.
  
  這是在實(shí)際項目中用到的一個(gè)特性,為了盡可能少的修改原有的代碼使用了這種折中的做法.使用reflection時(shí)建議不要執行太過(guò)復雜的方法,可能會(huì )使你的action響應慢的.
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
struts1中幾種常用的Action
struts中DispatchAction的使用
《簡(jiǎn)述培訓的流程》
智能營(yíng)銷(xiāo)總部:編程教學(xué) myBatis源碼解析
Struts DispatchAction,表單帶method參數選擇執行方法
CSDN技術(shù)中心 Struts心得—DispatchAction使用日記
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久