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

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

html-Cancel標簽應用注意事項篇

2019-11-18 13:10:07
字體:
來源:轉載
供稿:網友

  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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东城区| 西乡县| 山阳县| 旬阳县| 商河县| 清原| 宝山区| 廉江市| 老河口市| 神木县| 武山县| 阿拉善右旗| 安徽省| 蕉岭县| 茌平县| 谢通门县| 千阳县| 凤台县| 西贡区| 武鸣县| 库尔勒市| 基隆市| 郑州市| 廉江市| 翁源县| 洛隆县| 泾川县| 安塞县| 福贡县| 饶平县| 太湖县| 崇信县| 平顶山市| 香港| 会泽县| 安龙县| 健康| 盘锦市| 东山县| 始兴县| 平和县|