SSH结构中的一个bean的代码(JbxxXzsm.java),分别对应数据库中的每个字段
public class JbxxXzsm implements java.io.Serializable {private String xzsmmc;
...
public String getXzsmmc() {
return this.xzsmmc;
}
public void setXzsmmc(String xzsmmc) {
this.xzsmmc = xzsmmc;
}
......JbxxXzsmForm,Form中的代码:
   private JbxxXzsm xzsm;    public JbxxXzsm getXzsm() {
        return xzsm;
    }    public void setXzsm(JbxxXzsm xzsm) {
        this.xzsm = xzsm;
    }
    
    public XzsmForm()
    {
        super();
        xzsm = new JbxxXzsm();
    }然后在Action中,xzsmForm.getXzsm()就直接取得Form中(对应jsp页面中文本框之类表单元素)的值了
        XzsmForm xzsmForm = (XzsmForm) form;
       JbxxXzsm xzsm = xzsmForm.getXzsm();请问JbxxXzsmForm中的代码是怎么工作的?为什么不象bean中一样用set,get封装页面每个文本框之类的表单元素?

解决方案 »

  1.   

    LZ看看源码吧理解可能更深些
    package org.apache.struts.action;
    import org.apache.struts.upload.MultipartRequestHandler;
    import javax.servlet.ServletRequest;
    import javax.servlet.http.HttpServletRequest;
    import java.io.Serializable;
    public abstract class ActionForm implements Serializable {
        // ----------------------------------------------------- Instance Variables
        /**
         * <p>The servlet instance to which we are attached.</p>
         */
        protected transient ActionServlet servlet = null;
        /**
         * <p>The MultipartRequestHandler for this form, can be
         * <code>null</code>.</p>
         */
        protected transient MultipartRequestHandler multipartRequestHandler;
          /**
         * <p>Return the servlet instance to which we are attached.</p>
         *
         * @return The servlet instance to which we are attached.
         */
        protected ActionServlet getServlet() {
            return (this.servlet);
        }
        /**
         * <p>Return the controller servlet instance to which we are attached. as
         * an <code>ActionServletWrapper</code>.</p>
         *
         * @return An instance of {@link ActionServletWrapper}
         * @see ActionServletWrapper
         * @since Struts 1.0.1
         */
        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;
        }
        /**
         * <p>Set the servlet instance to which we are attached (if
         * <code>servlet</code> is non-null).</p>
         *
         * @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
        /**
         * <p>>Can be used to reset all bean properties to their default state.
         * This method is called before the properties are repopulated by the
         * controller.</p>
         *
         * <p>The default implementation attempts to forward to the HTTP version
         * of this method.</p>
         *
         * @param mapping The mapping used to select this instance
         * @param request The servlet request we are processing
         */
        public void reset(ActionMapping mapping, ServletRequest request) {
            try {
                reset(mapping, (HttpServletRequest) request);
            } catch (ClassCastException e) {
                ; // FIXME: Why would this ever happen except a null
            }
        }
        /**
         * <p>Can be used to reset bean properties to their default state, as
         * needed.  This method is called before the properties are repopulated by
         * the controller.</p>
         *
         * <p>The default implementation does nothing. In practice, the only
         * properties that need to be reset are those which represent checkboxes
         * on a session-scoped form. Otherwise, properties can be given initial
         * values where the field is declared. </p>
         *
         * <p>If the form is stored in session-scope so that values can be
         * collected over multiple requests (a "wizard"), you must be very careful
         * of which properties, if any, are reset. As mentioned, session-scope
         * checkboxes must be reset to false for any page where this property is
         * set. This is because the client does not submit a checkbox value when
         * it is clear (false). If a session-scoped checkbox is not proactively
         * reset, it can never be set to false.</p>
         *
         * <p>This method is <strong>not</strong> the appropriate place to
         * initialize form value for an "update" type page (this should be done in
         * a setup Action). You mainly need to worry about setting checkbox values
         * to false; most of the time you can leave this method unimplemented.
         * </p>
         *
         * @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
        }
        /**
         * <p>Can be used to validate the properties that have been set for this
         * non-HTTP request, and return an <code>ActionErrors</code> object that
         * encapsulates any validation errors that have been found. If no errors
         * are found, return <code>null</code> or an <code>ActionErrors</code>
         * object with no recorded error messages.</p>
         *
         * <p>The default implementation attempts to forward to the HTTP version
         * of this method.</p>
         *
         * @param mapping The mapping used to select this instance
         * @param request The servlet request we are processing
         * @return The set of validation errors, if validation failed; an empty
         *         set or <code>null</code> if validation succeeded.
         */
        public ActionErrors validate(ActionMapping mapping, ServletRequest request) {
            try {
                return (validate(mapping, (HttpServletRequest) request));
            } catch (ClassCastException e) {
                return (null);
            }
        }
        /
         * <p>The default implementation performs no validation and returns
         * <code>null</code>. Subclasses must override this method to provide any
         * validation they wish to perform.</p>
         *
         * @param mapping The mapping used to select this instance
         * @param request The servlet request we are processing
         * @return The set of validation errors, if validation failed; an empty
         *         set or <code>null</code> if validation succeeded.
         * @see DynaActionForm
         */
        public ActionErrors validate(ActionMapping mapping,
            HttpServletRequest request) {
            return (null);
        }
      

  2.   

    LZ看看源码吧理解可能更深些/*
     * $Id: ActionForm.java 471754 2006-11-06 14:55:09Z husted $
     *
     * Licensed to the Apache Software Foundation (ASF) under one
     * or more contributor license agreements.  See the NOTICE file
     * distributed with this work for additional information
     * regarding copyright ownership.  The ASF licenses this file
     * to you under the Apache License, Version 2.0 (the
     * "License"); you may not use this file except in compliance
     * with the License.  You may obtain a copy of the License at
     *
     *  http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing,
     * software distributed under the License is distributed on an
     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     * KIND, either express or implied.  See the License for the
     * specific language governing permissions and limitations
     * under the License.
     */
    package org.apache.struts.action;import org.apache.struts.upload.MultipartRequestHandler;import javax.servlet.ServletRequest;
    import javax.servlet.http.HttpServletRequest;import java.io.Serializable;/**
     * <p>An <strong>ActionForm</strong> is a JavaBean optionally associated with
     * one or more <code>ActionMappings</code>. Such a bean will have had its
     * properties initialized from the corresponding request parameters before the
     * corresponding <code>Action.execute</code> method is called.</p>
     *
     * <p>When the properties of this bean have been populated, but before the
     * <code>execute</code> method of the <code>Action</code> is called, this
     * bean's <code>validate</code> method will be called, which gives the bean a
     * chance to verify that the properties submitted by the user are correct and
     * valid. If this method finds problems, it returns an error messages object
     * that encapsulates those problems, and the controller servlet will return
     * control to the corresponding input form. Otherwise, the
     * <code>validate</code> method returns <code>null</code>, indicating that
     * everything is acceptable and the corresponding <code>Action.execute</code>
     * method should be called.</p>
     *
     * <p>This class must be subclassed in order to be instantiated. Subclasses
     * should provide property getter and setter methods for all of the bean
     * properties they wish to expose, plus override any of the public or
     * protected methods for which they wish to provide modified functionality.
     * </p>
     *
     * <p>Because ActionForms are JavaBeans, subclasses should also implement
     * <code>Serializable</code>, as required by the JavaBean specification. Some
     * containers require that an object meet all JavaBean requirements in order
     * to use the introspection API upon which ActionForms rely.</p>
     *
     * @version $Rev: 471754 $ $Date: 2005-11-12 08:14:24 -0500 (Sat, 12 Nov 2005)
     *          $
     */
    public abstract class ActionForm implements Serializable {
        // ----------------------------------------------------- Instance Variables    /**
         * <p>The servlet instance to which we are attached.</p>
         */
        protected transient ActionServlet servlet = null;    /**
         * <p>The MultipartRequestHandler for this form, can be
         * <code>null</code>.</p>
         */
        protected transient MultipartRequestHandler multipartRequestHandler;    // ------------------------------------------------------------- Properties    /**
         * <p>Return the servlet instance to which we are attached.</p>
         *
         * @return The servlet instance to which we are attached.
         */
        protected ActionServlet getServlet() {
            return (this.servlet);
        }    /**
         * <p>Return the controller servlet instance to which we are attached. as
         * an <code>ActionServletWrapper</code>.</p>
         *
         * @return An instance of {@link ActionServletWrapper}
         * @see ActionServletWrapper
         * @since Struts 1.0.1
         */
        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;
        }    /**
         * <p>Set the servlet instance to which we are attached (if
         * <code>servlet</code> is non-null).</p>
         *
         * @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;
        }
      

  3.   

    1、检查Action的映射,确定Action中已经配置了对ActionForm中的映射
    2、根据name属性,找到FormBean的配置信息
    3、检查Action的FormBean使用范围,确定在此范围下(Request,Session)是否已经有此FormBean的实例存在
    4、加入当前范围下已经存在了此FormBean的实例,而是对于当前请求来说,是同一种类型的话,那么就重用
    5、否则,就重新构造一个FormBean的实例(调用构造方法)并保存在一定作用范围下
    6、FormBean的reset()方法被调用
    7、调用对应的setter方法,对状态属性赋值
    8、如果Validate的属性为true,那么就调用FormBean的Validate()方法
    9、如果Validate()方法没有返回任何错误,控制器将ActionForm作为参数传给Action实例的execute()方法并执