Understanding the lifecycle of an ActionForm...

The ActionServlet handles requests for Struts, i.e., requests ending in *.do
is common. The ActionServlet looks up the RequestProcessor associated with
the module prefix. The RequestProcessor implements the handling of the
lifecycle and uses RequestUtils as a fa�ade to Servlets scopes.

When a form gets submitted, Struts looks up the action-mappings for the
current request path from the ModuleConfig. The ModuleConfig is the object
manifestation of the struts-config.xml file, each module gets its own
ModuleConfig. (Recall that the action attribute in the html:form specifies
the path of the action to invoke.)

Struts locates the form-bean mapping from the action-mapping associated with
the request path. (This occurs in the processActionForm method of the
RequestProcessor by calling the createActionForm method of RequestUtils.)
If Struts does not find an ActionForm in the scope specified by the
action-mapping, it will create an instance of the ActionForm identified
form-bean mapping.

Struts populates the ActionForm by mapping the request parameters from the
HTML form variables to the JavaBean properties of the ActionForm instance.
Before it populates the form, Struts calls the reset method of the
ActionForm. (This occurs in the processPopulate method of the
RequestProcessor by calling the populate method of RequestUtils.)

If validation is required and is successful an instance of the Action class
execute method will be invoked. Validation is required if the validate
attribute of the action mapping is not false (the default is true). If
validation fails, control will be returned to the submitting form (the input
JSP) where the JSP form fields will be populated by the ActionForm. The
ActionForm is valid if the validate method returns null or an empty
ActionErrors collection. (This occurs in the processValidate method of the
RequestProcessor.)

The best way to understand the lifecycle is read the source code for the
RequestProcessor.

commnents below....

Rick Hightower
Developer

Struts/J2EE training -- http://www.arc-mind.com/strutsCourse.htm

Struts/J2EE consulting --
http://www.arc-mind.com/consulting.htm#StrutsMentoring


-----Original Message-----
From: Rodney Paul [mailto:[EMAIL PROTECTED]
Sent: Tuesday, January 13, 2004 2:47 AM
To: Struts Users Mailing List (E-mail)
Subject: ActionForm LifeCycle (Maybe a bug in Struts)


Hi All,

I was wondering if anyone knew what the lifecycle of an ActionForm is?
The reason I ask is in regards to the display of variables with a
<html:form>.

I have noticed that the values displayed within a <html:form> are values
after a reset() method has been called,

*** The reset method gets called on each request. Your job is to populate
the form either in the reset method with default values or populate the
actionForm in an action and forward to the JSP page that has the html:form
to populate application specific (look up in a database).

and not after a validate method is called.

**Validate is for form submission not a mechanism to populate a form. You
are not using the validate method correctly.

Can anyone tell me the reason for this, as I seem to think this is a bug
within the Struts framework.

** Sorry. It is not. You need to get a good book on Struts.... I suggest
Professional Struts by James Goodwill and me. ;)


An example follows as to what I am talking about (based upon the following
ActionForm):


// Example ActionForm
public class MyActionForm extends ActionForm {
    private String maximumDocuments = "3";
    ...    // More variables


    public MyActionForm() {
        super();
        System.out.println("Constructor called");
    }



    public void setMaximumDocuments(String maximumDocuments) {
        this.maximumDocuments = maximumDocuments;
    }

    public String getMaximumDocuments() {
        return maximumDocuments;
    }
    ...    // More get/set methods


    public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
        ActionErrors errors = new ActionErrors();

        System.out.println("validate called");
        System.out.println("before processing maximumDocuments: " +
maximumDocuments);

        if(maximumDocuments == null) {
            maximumDocuments = "50";    // The supposed default value (if no
value entered)
        }

******************************** BAD MOJO

        System.out.println("after processing maximumDocuments: " +
maximumDocuments);
                ...    // Say an error has been returned
                return errors;
    }

    public void reset(ActionMapping mapping, HttpServletRequest request) {
        System.out.println("reset called");
        System.out.println("before processing maximumDocuments: " +
this.maximumDocuments);

        this.maximumDocuments   = "50";

        System.out.println("after processign maximumDocuments: " +
this.maximumDocuments);
        }
}



SCENARIOS:



----------------------------------------------------------------------------
--------------------------------------------
LIFECYCLE 1: From a previous page to a form which uses this ActionForm

The following output gets displayed:

Constructor called
reset called
before processing maximumDocuments: 3
after processing maximumDocuments: 50

RESULT: Here the value displayed in the form is 50 for maximumDocuments
(Correct)
----------------------------------------------------------------------------
--------------------------------------------

----------------------------------------------------------------------------
----------------------------------------------------------------------------
------------------
LIFECYCLE 2: If an error occurs within the ActionForm (and the value 1 has
been entered for maximumDocuments)

The following output gets displayed:

Constructor called
reset called
before processing maximumDocuments: 3
after processing maximumDocuments: 50
validate called
before processing maximumDocuments: 1
after processing maximumDocuments: 1

RESULT: Here the value displayed in the form is 50 for maximumDocuments
(Incorrect as it should be 1)
----------------------------------------------------------------------------
----------------------------------------------------------------------------
------------------



Can anyone please tell me why this is happening, and how to correct this
problem.

Cheers
Rodneyt Paul



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to