Eddie:

   Thanks for the "enlightenment"!  I think I understand what you're saying
about a contract.

   With Struts 1.0, whenever we needed to pre-populate a form bean, our
actions would usually instantiate the form bean instead of letting Struts do
it.  That way we could pre-populate right there in the action before
forwarding to the JSP.  This was option was available since we weren't using
dyna forms.  Everything was done in a single action class.    
  
   However, this is really a bad practice.  Instantiation of the form beans
should be left to Struts.  Therefore, whenever we encounter a situation
where we need to pre-populate a form bean, we should create a second action
who's sole responsibility is to handle pre-population.  This second action
would be forwarded to by the first action.  For the second action, Struts
will create the form bean as specified in the struts config file -- with
validate usually set to false.
  
   I did figure out to have the action instantiate a form bean based on a
dyna form declaration.  Specifically, I could use code similar to the
following:

    dynaForm = (DynaValidatorForm) 
 
DynaActionFormClass.createDynaActionFormClass("employeeForm").newInstance();
    dynaForm.set("name", "Eddie");

   But again, this is bad practice.  As a matter of fact, when I was reading
the Javadocs for the "DynaActionFormClass", I saw the comment:

        "NOTE - This class is only used in the internal implementation 
         of dynamic action form beans. Applications never need to consult 
         this documentation."

   That's when I first became suspicious that I was doing something wrong
and decided to consult the Struts Users mailing list.

Thanks again,

JOHN 
   
-----Original Message-----
From: Eddie Bush [mailto:[EMAIL PROTECTED]]
Sent: Friday, September 27, 2002 2:20 PM
To: Struts Users Mailing List
Subject: Re: HELP!: Pre-populating A DynaValidatorForm in Struts 1.1 B2


John,

What you need to do is pre-face the page with a "populate" action.  This 
action (see comment below) casts the form to a DynaValidatorForm and 
then calls form.set("property", value) for each property you need to 
set.  Once this is done, you just forward to the JSP.  This works :-) 
 If it doesn't work for you, it's a mistake or a bug.  Let us know if 
you're unable to get it working still ...

Hohlen, John wrote:

>Does anyone know how to instantiate a "DynaValidatorForm" declared in the
>"validation.xml" file within Java code?
>
>  <formset>   
>    <form name="employeeForm">
>      <field property="emailAddress"
>             depends="required,email">
>        <arg0 key="general.emailAddress"/>       
>      </field>
>      <field property="anniversaryDate"
>             depends="required,date">
>        <arg0 key="general.anniversaryDate"/>
>        <var>
>          <var-name>datePatternStrict</var-name>
>          <var-value>MM/dd/yyyy</var-value>
>        </var>
>      </field>
>   </form>            
> </formset>   
>
>More specifically, I want this form, used by my JSP, to contain
>pre-populated data from the database.  Therefore, it is desired to have my
>action to instantiate the form bean and pre-populate it -- versus letting
>the Struts framework create one with default values.
>
No, no.  Actions/Forms have a contract -- the form *will* be created. 
 You should not take this upon yourself.  Expect it to be there -- if it 
is not NPE and fail-fast (and submit a bug report!).

>In Struts 1.0, I just
>did the following in my action:
>
> EmployeeForm employeeForm = new EmployeeForm();
>
>But this won't work using dyna forms in Struts 1.1 since there is no
>"EmployeeForm" Java class.
>
>Thanks!
>

-- 
Eddie Bush




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

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

Reply via email to