熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Struts源碼研究 - html-Link標簽篇

2022-06-13   來源: Java開源技術 

  Struts裡的html:Cancel標簽是在Form中經常運用的一個標簽主要功能就是cancel當前Form一般寫法如下
  
  ===========================
  <html:cancel>
  <bean:message key=createusercancelbutton/>
  </html:cancel>
  ===========================
  
  這個標簽將生成如下的HTML代碼
  <input type=submit name=orgapaclCANCEL value=返回 onclick=bCancel=true;>
  bCancel=true是一段javascriptbCancel是在使用Struts的Validator時Struts自動為我們加的一段Javascript代碼裡的一個變量
  這段Javascript簡略摘要如下
  
  ===========================
  <script type=text/javascript language=Javascript>
  
  <! Begin
  
  var bCancel = false;
  
  function validateCreateUserForm(form) {
  if (bCancel)
  return true;
  else
  return validateMaxLength(form) && validateRequired(form) && validateMinLength(form);
  }
  
  ===========================
  
  由上可以看到這個bCancel=true時Javascript將自動將表單提交(return true)也就是說如果我們在後台Action的代碼裡
  沒有對這個Cancel動作寫特定代碼的話這個Cancel標簽產生的效果和submit按鈕產生的動作完全一致!!(因為這個按鈕的
  type也等於submit)
  這一點需要非常的注意!所以一般來說我們在Action類的execute方法裡面會加上如下的一段代碼來處理這個Cancel動作
  
  ===========================
  // Was this transaction cancelled?
  if (isCancelled(request)) {
  return (mappingfindForward(createusersuccess));
  }
  ===========================
  
  有了以上的代碼Cancel動作就有了相應的處理代碼轉到相關的頁面了
  本來事情已經解決但本著對Struts源碼研究的精神我們還需要對以上代碼研究一下
  OK讓我們來看一下isCancelled這個方法在什麼地方被定義了內容是什麼?
  首先發現這個方法被定義在Action類裡面代碼如下
  
  ===========================
  /**
  * <p>Returns <code>true</code> if the current forms cancel button was
  * pressed This method will check if the <code>GlobalsCANCEL_KEY</code>
  * request attribute has been set which normally occurs if the cancel
  * button generated by <strong>CancelTag</strong> was pressed by the user
  * in the current request If <code>true</code> validation performed
  * by an <strong>ActionForm</strong>s <code>validate()</code> method
  * will have been skipped by the controller servlet</p>
  *
  * @param request The servlet request we are processing
  * @see orgapaclCancelTag
  */
  protected boolean isCancelled(HttpServletRequest request) {
  
  return (requestgetAttribute(GlobalsCANCEL_KEY) != null);
  
  }
  ===========================
  
  哦原來是在request對象中查找GlobalsCANCEL_KEY這個key值是否綁定了一個對象如果是那麼就代表按下Cancel按鈕後
  Struts會在request對象中綁定一個對象並以這個key值來命名
  那Struts是在什麼地方綁定了這個對象呢?很自然的讓我們從頭找起
  從ActionServlet的process方法開始找起歷經多次方法調用終於找到了根源原來是在RequestProcessorjava中代碼如下
  
  ===========================
  /**
  * <p>Process an <code>HttpServletRequest</code> and create the
  * corresponding <code>HttpServletResponse</code></p>
  *
  * @param request The servlet request we are processing
  * @param response The servlet response we are creating
  *
  * @exception IOException if an input/output error occurs
  * @exception ServletException if a processing exception occurs
  */
  public void process(HttpServletRequest request
  HttpServletResponse response)
  throws IOException ServletException {
  
  //省略代碼若干
  // Process any ActionForm bean related to this request
  ActionForm form = processActionForm(request response mapping);
  //答案就在這個processPopulate方法中
  processPopulate(request response form mapping);
  if (!processValidate(request response form mapping)) {
  return;
  }
  
  /**
  * Populate the properties of the specified ActionForm instance from
  * the request parameters included with this request In addition
  * request attribute <code>GlobalsCANCEL_KEY</code> will be set if
  * the request was submitted with a button created by
  * <code>CancelTag</code>
  *
  * @param request The servlet request we are processing
  * @param response The servlet response we are creating
  * @param form The ActionForm instance we are populating
  * @param mapping The ActionMapping we are using
  *
  * @exception ServletException if thrown by RequestUtilspopulate()
  */
  protected void processPopulate(HttpServletRequest request
  HttpServletResponse response
  ActionForm form
  ActionMapping mapping)
  throws ServletException {
  
  if (form == null) {
  return;
  }
  
  // Populate the bean properties of this ActionForm instance
  if (logisDebugEnabled()) {
  logdebug( Populating bean properties from this request);
  }
  
  formsetServlet(thisservlet);
  formreset(mapping request);
  
  if (mappinggetMultipartClass() != null) {
  requestsetAttribute(GlobalsMULTIPART_KEY
  mappinggetMultipartClass());
  }
  
  RequestUtilspopulate(form mappinggetPrefix() mappinggetSuffix()
  request);
  
  // Set the cancellation request attribute if appropriate
  if ((requestgetParameter(ConstantsCANCEL_PROPERTY) != null) ||
  (requestgetParameter(ConstantsCANCEL_PROPERTY_X) != null)) {
  
  requestsetAttribute(GlobalsCANCEL_KEY BooleanTRUE);
  }
  }
  
  ===========================
  
  OK看最後幾行代碼Struts從request中取得ConstantsCANCEL_PROPERTY這個參數如果這個參數不為空那麼他就將
  TRUE這個對象以GlobalsCANCEL_KEY為key值放到了request對象中
  至於這個ConstantsCANCEL_PROPERTY這個值是什麼現在都可以猜到了顯然就是html:Cancel這個標簽生成的HTML代碼
  中Cancel這個按鈕的名稱嘛!查了一下果然是
  
  <input type=submit name=orgapaclCANCEL value=返回 onclick=bCancel=true;>
  而ConstantsCANCEL_PROPERTY這個值就是orgapaclCANCEL

From:http://tw.wingwit.com/Article/program/Java/ky/201311/28930.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.