posts - 10, comments - 9, trackbacks - 0, articles - 17

          struts源碼研究3 ActionForm

          Posted on 2008-12-09 21:48 wesley1987 閱讀(687) 評論(0)  編輯  收藏 所屬分類: struts源碼學習

          ActionForm 源碼+注釋翻譯。
          郁悶,第一次翻譯的文件被我弄丟了,又重來了一遍。

          import org.apache.struts.upload.MultipartRequestHandler;

          import javax.servlet.ServletRequest;
          import javax.servlet.http.HttpServletRequest;

          import java.io.Serializable;

          /**
           * 一個ActionForm是可以與一個或多個任意的ActionMapping關聯(lián)的JavaBean。這個bean的里屬性
           * 在對應Action.execute方法調用前,由對應的request初始化。    
           *
           * 當這個bean的屬性被賦值后,Action.execute方法調用之前,bean的validate方法
           * 將被調用,這個方法用來校驗用戶提交的屬性值。如果發(fā)現(xiàn)錯誤,就返回一個包含了這些錯誤內容的
           * error信息。controller將返回至相應的輸入表單。如果無錯誤,validate方法就返回null,
           * 相應的Action.execute方法將被調用。
           * 
           * 這個類必須被繼承使用。子類要為所有會被公開(expose)的bean屬性提供get和set方法,以及
           * 重寫一些public或protected方法以提供具體實現(xiàn)。
           * 
           * 由于ActionForm是JavaBeans,所以根據(jù)JavaBeans的規(guī)范,子類也會實現(xiàn)Serializable(可序列
           * 化)接口。為了使用ActionForm相關的introspection API ?,一些容器會要求一個Form對象滿足
           * 所有JavaBean規(guī)范。
           * 
           
          */
          public abstract class ActionForm implements Serializable {
              
          // ----------------------------------------------------- Instance Variables

              
          /**
               * <p>The servlet instance to which we are attached.</p>
               
          */
              
          protected transient ActionServlet servlet = null;

              
          /**
               * MultipartRequestHandler
               * 這個form的多請求處理對象。        //transient表示該屬性將不被序列化。
               *  //wzl注:該對象的作用大約是結合upload包里的類,實現(xiàn)文件的上傳功能。
               *  //由于還沒接觸過,所以下面的相關方法不再翻譯
               
          */
              
          protected transient MultipartRequestHandler multipartRequestHandler;

              
          // ------------------------------------------------------------- Properties

              
          /**
               * 
          @return The servlet instance to which we are attached.
               
          */
              
          protected ActionServlet getServlet() {
                  
          return (this.servlet);
              }

              
          /**
               * ActionServletWrapper
               * 將所屬的控制器servlet以ActionServletWrapper對象返回。
               * //該對象也在servlet上提供了對MultipartRequestHandler的使用。
               * 
          @return An instance of ActionServletWrapper
               
          */
              
          public ActionServletWrapper getServletWrapper() {
                  
          return new ActionServletWrapper(getServlet());
              }

              
          /**
               * <p>Return the <code>MultipartRequestHandler</code> for this form The
               * reasoning behind this is to give form bean developers control over the
               * lifecycle of their multipart requests through the use of the
               * <code>finish</code> and/or <code>rollback</code> methods of
               * <code>MultipartRequestHandler</code>.  This method will return
               * <code>null</code> if this form's enctype is not "multipart/form-data".
               * </p>
               * 
               * 
          @return The {@link org.apache.struts.upload.MultipartRequestHandler}
               *         for this form.
               * 
          @see org.apache.struts.upload.MultipartRequestHandler
               
          */
              
          public MultipartRequestHandler getMultipartRequestHandler() {
                  
          return multipartRequestHandler;
              }

              
          /**
               * 設定所屬的servlet(若非空)實例。
               *
               * 
          @param servlet The new controller servlet, if any
               
          */
              
          public void setServlet(ActionServlet servlet) {
                  
          this.servlet = servlet;

                  
          // :FIXME: Should this be releasing resources?
              }

              
          /**
               * <p>Set the Handler provided for use in dealing with file uploads.</p>
               *
               * 
          @param multipartRequestHandler The Handler to use for fileuploads.
               
          */
              
          public void setMultipartRequestHandler(
                  MultipartRequestHandler multipartRequestHandler) {
                  
          this.multipartRequestHandler = multipartRequestHandler;
              }

              
          // --------------------------------------------------------- Public Methods

              
          /**
               *  //直接調用下面那個重載HTTP的。
               
          */
              
          public void reset(ActionMapping mapping, ServletRequest request) {
                  
          try {
                      reset(mapping, (HttpServletRequest) request);
                  } 
          catch (ClassCastException e) {
                      ; 
          // FIXME: Why would this ever happen except a null
                  }
              }

              
          /**
               * 用來重置bean中的屬性。這個方法在屬性被控制器賦值前調用。
               * 
               * 默認的方法體為空,實際中發(fā)現(xiàn),唯一需要被重置的是聲明到session中表單的checkboxs屬性
               * 除此以外的屬性,都會在域聲明時被初始化。
               * 
               * 如果為了實現(xiàn)form能被多個請求取值而將其放到了session中,那么你必須非常注意那些被
               * 重置(reset)的值,如上面提到的,對于每一個輸入表單內容的頁面,必須將session范圍中的
               * checkboxs提前重置為fales。這是因為只有當checkbox為fales時才表示客戶沒有提交該值。
               * 如果一個session中的checkbox沒有被提前重置的話,他將永遠不會為fales。
               * 
               * 這個方法不適合用來給"修改"類型的頁面中的表單賦初始值(這應該在setup Action中)。你唯一需要
               * 關心的就是將checkbox的值改為fales。所以這個方法一般不用實現(xiàn)
               * 
               * 
          @param mapping The mapping used to select this instance
               * 
          @param request The servlet request we are processing
               
          */
              
          public void reset(ActionMapping mapping, HttpServletRequest request) {
                  
          // Default implementation does nothing
              }

              
          /**
               *  //直接調用下面那個重載HTTP的。
               
          */
              
          public ActionErrors validate(ActionMapping mapping, ServletRequest request) {
                  
          try {
                      
          return (validate(mapping, (HttpServletRequest) request));
                  } 
          catch (ClassCastException e) {
                      
          return (null);
                  }
              }

              
          /**
               * 用來驗證request中的屬性值,并返回一個ActionErrors對象,它包含了驗證中發(fā)現(xiàn)的錯誤信息。
               * 如果驗證成功,則返回null 或一個無錯誤信息記錄的ActionErrors對象。
               * 
               * 默認的執(zhí)行體為空并返回null,子類中必須重寫這個方法以提供需要的驗證操作。
               * 
               * 
          @param mapping The mapping used to select this instance
               * 
          @param request The servlet request we are processing
               * 
          @return 驗證失敗就返回錯誤信息; 驗證成功則返回null或空的信息。
               *         
               * 
          @see DynaActionForm
               
          */
              
          public ActionErrors validate(ActionMapping mapping,
                  HttpServletRequest request) {
                  
          return (null);
              }
          }
          主站蜘蛛池模板: 诸暨市| 蒙山县| 台中市| 县级市| 云龙县| 惠东县| 兖州市| 六安市| 安阳县| 灵台县| 越西县| 大英县| 靖远县| 桦甸市| 应用必备| 乌拉特后旗| 杂多县| 格尔木市| 大安市| 宜黄县| 诏安县| 新郑市| 景德镇市| 邮箱| 华宁县| 安图县| 自治县| 霸州市| 贵州省| 敖汉旗| 陆丰市| 都安| 密山市| 改则县| 保亭| 德兴市| 云霄县| 奉新县| 桦甸市| 石狮市| 建昌县|