Struts源码分析3:ActionForm

by sundy 8/7/2009 11:56:30 AM

前言:发现很多j2ee的程序员基础都差得不行 ,会用ssh的甚至都不知道为何要这样,以及如何设计的 。因此为大家简单的剖析一下Struts的设计 ,以资借鉴 。

import org.apache.struts.upload.MultipartRequestHandler;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
/**
* 一个ActionForm是可以与一个或多个任意的ActionMapping关联的JavaBean。这个bean的里属性
* 在对应Action.execute方法调用前,由对应的request初始化。    
*
* 当这个bean的属性被赋值后,Action.execute方法调用之前,bean的validate方法
* 将被调用,这个方法用来校验用户提交的属性值。如果发现错误,就返回一个包含了这些错误内容的
* error信息。controller将返回至相应的输入表单。如果无错误,validate方法就返回null,
* 相应的Action.execute方法将被调用。

* 这个类必须被继承使用。子类要为所有会被公开(expose)的bean属性提供get和set方法,以及
* 重写一些public或protected方法以提供具体实现。

* 由于ActionForm是JavaBeans,所以根据JavaBeans的规范,子类也会实现Serializable(可序列
* 化)接口。为了使用ActionForm相关的introspection API ?,一些容器会要求一个Form对象满足
* 所有JavaBean规范。

*/
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包里的类,实现文件的上传功能。
     *  //由于还没接触过,所以下面的相关方法不再翻译
*/
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中的属性。这个方法在属性被控制器赋值前调用。
     * 
     * 默认的方法体为空,实际中发现,唯一需要被重置的是声明到session中表单的checkboxs属性
     * 除此以外的属性,都会在域声明时被初始化。
     * 
     * 如果为了实现form能被多个请求取值而将其放到了session中,那么你必须非常注意那些被
     * 重置(reset)的值,如上面提到的,对于每一个输入表单内容的页面,必须将session范围中的
     * checkboxs提前重置为fales。这是因为只有当checkbox为fales时才表示客户没有提交该值。
     * 如果一个session中的checkbox没有被提前重置的话,他将永远不会为fales。
     * 
     * 这个方法不适合用来给"修改"类型的页面中的表单赋初始值(这应该在setup Action中)。你唯一需要
     * 关心的就是将checkbox的值改为fales。所以这个方法一般不用实现
     * 
     * @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对象,它包含了验证中发现的错误信息。
     * 如果验证成功,则返回null 或一个无错误信息记录的ActionErrors对象。
     * 
     * 默认的执行体为空并返回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);
    }
}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Java & OpenSource Domain

Related posts

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2007 - 2008 Design by Sundy Linghua-Zhang 蜀ICP备08108648号

About the author

Name of author Author name
Something about me and what I do.

E-mail me Send mail

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2012

Sign in