程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> Struts源碼研究-logic-Iterator標簽篇

Struts源碼研究-logic-Iterator標簽篇

編輯:JAVA編程入門知識

  Struts裡的Html:Cancel標簽是在Form中經常運用的一個標簽,主要功能就是cancel當前Form,一般寫法如下:
  
  ===========================
  <html:cancel>
  <bean:message key="createuser.cancelbutton"/>
  </html:cancel>
  ===========================
  
  這個標簽將生成如下的HTML代碼:
  <input type="submit" name="org.apache.struts.taglib.html.CANCEL" value="返回" onclick="bCancel=true;">
  bCancel=true是一段javascript,bCancel是在使用Struts的Validator時,Struts自動為我們加的一段Javascript代碼裡的一個變量
  這段Javascript簡略摘要如下:
  
  ===========================
  <script type="text/javascript" language="Javascript1.1">
  
  <!-- 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 (mapping.findForward("createusersUCcess"));
  }
  ===========================
  
  有了以上的代碼,Cancel動作就有了相應的處理代碼,轉到相關的頁面了。本來事情已經解決,但本著對Struts源碼研究的精神,我們還需要對以上代碼研究一下
  OK,讓我們來看一下isCancelled這個方法在什麼地方被定義了,內容是什麼?
  首先發現,這個方法被定義在Action類裡面,代碼如下:
  
  ===========================
  /**
  * <p>Returns <code>true</code> if the current form's cancel button was
  * pressed. This method will check if the <code>Globals.CANCEL_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 org.apache.struts.taglib.html.CancelTag
  */
  protected boolean isCancelled(HttpServletRequest request) {
  
  return (request.getAttribute(Globals.CANCEL_KEY) != null);
  
  }
  ===========================
  
  原來是在request對象中查找Globals.CANCEL_KEY這個key值是否綁定了一個對象,假如是,那麼就代表按下Cancel按鈕後,
  Struts會在request對象中綁定一個對象,並以這個key值來命名那Struts是在什麼地方綁定了這個對象呢?很自然的,讓我們從頭找起從ActionServlet的process方法開始找起,歷經多次方法調用,終於找到了根源,原來是在RequestProcessor.java中,代碼如下:
  
  ===========================
  /**
  * <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>Globals.CANCEL_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 RequestUtils.populate()
  */
  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 (log.isDebugEnabled()) {
  log.debug(" Populating bean properties from this request");
  }
  
  form.setServlet(this.servlet);
  form.reset(mapping, request);
  
  if (mapping.getMultipartClass() != null) {
  request.setAttribute(Globals.MULTIPART_KEY,
  mapping.getMultipartClass());
  }
  
  RequestUtils.populate(form, mapping.getPrefix(), mapping.getSuffix(),
  request);
  
  // Set the cancellation request attribute if appropriate
  if ((request.getParameter(Constants.CANCEL_PROPERTY) != null)
  (request.getParameter(Constants.CANCEL_PROPERTY_X) != null)) {
  
  request.setAttribute(Globals.CANCEL_KEY, Boolean.TRUE);
  }
  }
  
  ===========================
  
  OK,看最後幾行代碼,Struts從request中取得Constants.CANCEL_PROPERTY這個參數,假如這個參數不為空,那麼他就將TRUE這個對象以Globals.CANCEL_KEY為key值,放到了request對象中至於這個Constants.CANCEL_PROPERTY這個值是什麼,現在都可以猜到了,顯然就是html:Cancel這個標簽生成的HTML代碼中,Cancel這個按鈕的名稱嘛!查了一下,果然是:
  
  <input type="submit" name="org.apache.struts.taglib.html.CANCEL" value="返回" onclick="bCancel=true;">
  而Constants.CANCEL_PROPERTY這個值就是org.apache.struts.taglib.html.CANCEL
 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved