You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Danko Desancic <dd...@northernobjects.com> on 2004/03/12 20:53:56 UTC

(pre)populating DynaActionForm

I am newbie to Struts and have a problem with  populating 
DynaActionForm. I have two actions: one to set up userForm and one to 
process it. 1st one is suposed to grab user info and populate the form. 
The second one updates user info after the form is submitted. The 
problem is that the from is not populated. Moreover changing the scope 
to "session" in action "/user/setUp"  results in form being populated 
with appropriate data. What am I doing wrong in here?

Thanks
Danko

from struts-config.xml
<form-bean name="userForm" 
type="org.apache.struts.action.DynaActionForm">that
            <form-property name="userName" type="java.lang.String"/>
            <form-property name="password" type="java.lang.String"/>
            <form-property name="fullName" type="java.lang.String"/>
            <form-property name="email" type="java.lang.String"/>
            <form-property name="status" type="java.lang.Integer" 
initial="0"/>
</form-bean>

<action
            path="/user/setUp"
            type="myPackage.user.SetUpUserAction"
            name="userForm"
            scope="request"
            validate="false">
            <forward name="Success"  
path="/WEB-INF/jsp/user/userForm.jsp"/>
</action>
       
<action
            path="/user/update"
            type="myPackage.user.UpdateUserAction"
            name="userForm"
            scope="request"
            validate="true"
            input="/WEB-INF/jsp/user/userForm.jsp">
            <forward name="Success"  path="/user/edit.do" redirect="true"/>
 </action>

from SetUpUserAction.execute()
            DynaActionForm userForm = (DynaActionForm)form;
           
            userForm.set("userName",user.getUserName());
            userForm.set("password",user.getPassword());
            userForm.set("fullName",user.getFullName());
            userForm.set("email",user.getEmail());
            userForm.set("status",new Integer(1));
                      
            forward = mapping.findForward("Success");
            return forward;


from userForm.jsp
<html:form action="/user/update.do">
<table cellpadding="2" cellspacing="2" border="1" width="800px">
    <tr>
        <td class="fieldHeader" colspan="4">UPDATE USER</td>
    </tr>
    <tr>
        <td class="fieldName">Username</td>
        <td><html:text property="userName"/></td>
    </tr>
   ......
    <tr>
        <td class="fieldName">Status</td>
        <td><html:radio property="status" value="1"/>Active &nbsp 
<html:radio property="status" value="0"/>Inactive </td>
    </tr>
    <tr>
        <td colspan="4" class="fieldName" align="center">
            <html:submit value="UPDATE"/>
        </td>
    </tr>
</table>
</html:form>


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: (pre)populating DynaActionForm

Posted by Joe Germuska <Jo...@Germuska.com>.
At 12:42 PM -0800 3/12/04, Hubert Rabago wrote:
>Thanks for the clarification, Joe.  The copy of RequestUtils I really studied
>was from late Dec/early Jan, so I just missed that method.  I remember
>reading the message you linked to and I've been meaning to ask, what did you
>mean by "prefilling forms with non-request data"?  Is it similar to one of
>the issues I'm trying to solve with the formdef
>http://www.rabago.net/struts/formdef/ plugin I'm working on?

It might be related to your feature for converting data from a 
business object to a form.

The general issue is this: user submits form; action processes; 
action forwards to another view, which has a different form.  The 
different form shouldn't be blank, it should be pre-filled with data. 
Say, for instance, the first form is a search form which returns a 
single record, and the second form is for editing the data returned 
by the search.

Since only one ActionForm is passed into the action for executing, 
there's no convenient way to get data into the "output" form.  You 
could have a page that retrieved values from request attributes, but 
then you can't use that same page/form for displaying validation 
errors to the user without copying the input form bean's properties 
into the same request attributes.

I just made a post to struts-dev to see if people wanted to engage in 
the "ViewController" discussion which popped up last fall. 
Interested parties are encouraged to join in over there.

Joe

PS this is the last msg on the old "ViewController" thread that I 
could dig up: 
http://thread.gmane.org/gmane.comp.jakarta.struts.devel/15486
It just kind of stalled out after that.  I think now that you don't 
have to have an ActionMapping to get a FormBean, it might be easier 
to implement the rest, as long as people can agree on a design.
-- 
Joe Germuska            
Joe@Germuska.com  
http://blog.germuska.com    
       "Imagine if every Thursday your shoes exploded if you tied them 
the usual way.  This happens to us all the time with computers, and 
nobody thinks of complaining."
             -- Jef Raskin

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: (pre)populating DynaActionForm

Posted by Hubert Rabago <ja...@yahoo.com>.
Thanks for the clarification, Joe.  The copy of RequestUtils I really studied
was from late Dec/early Jan, so I just missed that method.  I remember
reading the message you linked to and I've been meaning to ask, what did you
mean by "prefilling forms with non-request data"?  Is it similar to one of
the issues I'm trying to solve with the formdef
http://www.rabago.net/struts/formdef/ plugin I'm working on?  

Hubert

--- Joe Germuska <Jo...@Germuska.com> wrote:
> At 12:09 PM -0800 3/12/04, Hubert Rabago wrote:
> >As far as I can tell, there really isn't any support for prepopulating a
> >DynaActionForm.  The ff is from
>
>http://jakarta.apache.org/struts/userGuide/building_controller.html#dyna_action_form_classes
> >
> >"DynaActionForms are not a drop-in replacement for ActionForms. If you
> need
> >to access ActionForm properties in your Action, you will need to use the
> >map-style accessor, like myForm.get("name"). If you actively use the
> >ActionForm object in your Action, then you may want to use conventional
> >ActionForms instead.
> >
> >What I did was copy the code that RequestUtils uses to instantiate a
> >DynaActionForm, and put it in a method that I can call like this:
> >DynaActionForm dynaForm = createDynaActionForm("formName",
> >mapping.getModuleConfig());
> 
> Since 1/24/2004, RequestUtils has exposed a public method that allows 
> you to get an ActionForm instance using a FormBeanConfig and an 
> ActionServlet.  This is discussed in a bit of detail in 
> http://article.gmane.org/gmane.comp.jakarta.struts.user/78441
> 
> This method does not do any scope searching -- it just makes a new 
> instance -- but it helps.
> 
> I've been holding off on exposing any more of the form bean pieces 
> until I got some more feedback. I'm waiting to hear from some other 
> people who find it as annoying as I do to help elaborate the problem 
> and the solution (or waiting until it annoys me enough to come up 
> with a solution on my own...)   As noted in the above-referenced 
> post, we have an internal solution at work that puts us in a pretty 
> good spot relating to pre-filling forms, so the itch has been 
> scratched, so to speak.
> 
> Joe
> 
> -- 
> Joe Germuska            
> Joe@Germuska.com  
> http://blog.germuska.com    
>        "Imagine if every Thursday your shoes exploded if you tied them 
> the usual way.  This happens to us all the time with computers, and 
> nobody thinks of complaining."
>              -- Jef Raskin
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 


__________________________________
Do you Yahoo!?
Yahoo! Search - Find what you�re looking for faster
http://search.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: (pre)populating DynaActionForm

Posted by Joe Germuska <Jo...@Germuska.com>.
At 12:09 PM -0800 3/12/04, Hubert Rabago wrote:
>As far as I can tell, there really isn't any support for prepopulating a
>DynaActionForm.  The ff is from
>http://jakarta.apache.org/struts/userGuide/building_controller.html#dyna_action_form_classes
>
>"DynaActionForms are not a drop-in replacement for ActionForms. If you need
>to access ActionForm properties in your Action, you will need to use the
>map-style accessor, like myForm.get("name"). If you actively use the
>ActionForm object in your Action, then you may want to use conventional
>ActionForms instead.
>
>What I did was copy the code that RequestUtils uses to instantiate a
>DynaActionForm, and put it in a method that I can call like this:
>DynaActionForm dynaForm = createDynaActionForm("formName",
>mapping.getModuleConfig());

Since 1/24/2004, RequestUtils has exposed a public method that allows 
you to get an ActionForm instance using a FormBeanConfig and an 
ActionServlet.  This is discussed in a bit of detail in 
http://article.gmane.org/gmane.comp.jakarta.struts.user/78441

This method does not do any scope searching -- it just makes a new 
instance -- but it helps.

I've been holding off on exposing any more of the form bean pieces 
until I got some more feedback. I'm waiting to hear from some other 
people who find it as annoying as I do to help elaborate the problem 
and the solution (or waiting until it annoys me enough to come up 
with a solution on my own...)   As noted in the above-referenced 
post, we have an internal solution at work that puts us in a pretty 
good spot relating to pre-filling forms, so the itch has been 
scratched, so to speak.

Joe

-- 
Joe Germuska            
Joe@Germuska.com  
http://blog.germuska.com    
       "Imagine if every Thursday your shoes exploded if you tied them 
the usual way.  This happens to us all the time with computers, and 
nobody thinks of complaining."
             -- Jef Raskin

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: (pre)populating DynaActionForm

Posted by Niall Pemberton <ni...@blueyonder.co.uk>.
<html:form> looks at the action you identify checks if the form associated
with that action ("/user/update.do" in your case) exists in the scope
specified, and if it doesn't, creates it. Now the other html tags look for a
page scoped attribute under the default name if you don't specify the name
attribute - for example <html:text property="userName"/>.

So whats the scope of "userForm" on "/user/update.do" - is it the same as
"/user/setUp.do"?

Try adding the name attribute to your html tags:

   <html:text name="userForm" property="userName"/>.


or if your "scopes" are different for userForm on different actions - make
them all the same.

Niall


----- Original Message ----- 
From: "Danko Desancic" <dd...@northernobjects.com>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>
Sent: Friday, March 12, 2004 9:04 PM
Subject: Re: (pre)populating DynaActionForm


> This is really funny, I did output the request scope on the userForm.jsp
> and it turns out that Struts puts userForm in request scope on its own
> with all appropriate data that I set. The question is then why aren't
> they displayed in the html tags?
>
> Any ideas?
>
> Danko
>
>
> Niall Pemberton wrote:
>
> >Every thing looks fine to me. Is this the actual struts-config.xml you
are
> >trying to use - because I did notice your "/user/update" has re-direct in
> >the "Success" forward.  Obviously you say the problem is with the
> >"/user/setUp" action but I ask because a re-direct would cause the kind
of
> >behaviour you describe.
> >
> >Niall
> >
> >----- Original Message ----- 
> >From: "Danko Desancic" <dd...@northernobjects.com>
> >To: "Struts Users Mailing List" <st...@jakarta.apache.org>
> >Sent: Friday, March 12, 2004 8:26 PM
> >Subject: Re: (pre)populating DynaActionForm
> >
> >
> >
> >
> >>I tried that and it does not work. No errors though just balnk form with
> >>initial value.
> >>
> >>Thanks anyway
> >>
> >>Hubert Rabago wrote:
> >>
> >>
> >>
> >>>My bad.  I didn't read your mail through; only saw the top portion.
> >>>In your setupUserAction, after you prepopulate the form, set it as a
> >>>
> >>>
> >request
> >
> >
> >>>attribute:
> >>>
> >>>
> >>>
> >>>
> >>>
> >>>>>from SetUpUserAction.execute()
> >>>>
> >>>>
> >>>>>           DynaActionForm userForm = (DynaActionForm)form;
> >>>>>
> >>>>>           userForm.set("userName",user.getUserName());
> >>>>>           userForm.set("password",user.getPassword());
> >>>>>           userForm.set("fullName",user.getFullName());
> >>>>>           userForm.set("email",user.getEmail());
> >>>>>           userForm.set("status",new Integer(1));
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>               request.setAttribute("userForm",userForm);
> >>>
> >>>
> >>>
> >>>
> >>>>>           forward = mapping.findForward("Success");
> >>>>>           return forward;
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>It works when the form is in session scope because that will make the
> >>>
> >>>
> >whole
> >
> >
> >>>session deal with one form instance.
> >>>
> >>>hth,
> >>>Hubert
> >>>
> >>>--- Hubert Rabago <ja...@yahoo.com> wrote:
> >>>
> >>>
> >>>
> >>>
> >>>>As far as I can tell, there really isn't any support for prepopulating
a
> >>>>DynaActionForm.  The ff is from
> >>>>
> >>>>
> >>>>
> >>>>
> >>>>
>
>>http://jakarta.apache.org/struts/userGuide/building_controller.html#dyna_a
c
> >>
> >>
> >tion_form_classes
> >
> >
> >>>
> >>>
> >>>>"DynaActionForms are not a drop-in replacement for ActionForms. If you
> >>>>
> >>>>
> >need
> >
> >
> >>>>to access ActionForm properties in your Action, you will need to use
the
> >>>>map-style accessor, like myForm.get("name"). If you actively use the
> >>>>ActionForm object in your Action, then you may want to use
conventional
> >>>>ActionForms instead.
> >>>>
> >>>>"DynaActionForms cannot be instantiated using a no-argument
constructor.
> >>>>
> >>>>
> >In
> >
> >
> >>>>order to simulate the extra properties, there is a lot of machinery
> >>>>involved
> >>>>in their construction. You must rely on Struts to instantiate a
> >>>>DynaActionForm for you, via the ActionMapping."
> >>>>
> >>>>What I did was copy the code that RequestUtils uses to instantiate a
> >>>>DynaActionForm, and put it in a method that I can call like this:
> >>>>DynaActionForm dynaForm = createDynaActionForm("formName",
> >>>>mapping.getModuleConfig());
> >>>>
> >>>>
> >>>>
> >>>>   /**
> >>>>    * <p>Create the {@link DynaActionForm} instance identified by the
> >>>>given
> >>>>    * formName.</p>
> >>>>    *
> >>>>    * @param formName the name used to identify the form
> >>>>    * @param moduleConfig the configuration for the current module
> >>>>    * @return the DynaActionForm identified by formName
> >>>>    */
> >>>>   public static DynaActionForm createDynaActionForm(
> >>>>           String formName,
> >>>>           ModuleConfig moduleConfig) {
> >>>>       DynaActionForm dynaForm;
> >>>>       try {
> >>>>           // the code here is based on [copied from? :) ]
> >>>>           //  RequestUtils.createActionForm()
> >>>>           FormBeanConfig formBeanConfig =
> >>>>                   moduleConfig.findFormBeanConfig(formName);
> >>>>
> >>>>           DynaActionFormClass dynaClass =
> >>>>                   DynaActionFormClass.createDynaActionFormClass(
> >>>>                           formBeanConfig);
> >>>>
> >>>>           dynaForm = (DynaActionForm) dynaClass.newInstance();
> >>>>
> >>>>           if (logger.isTraceEnabled()) {
> >>>>               logger.trace("inside findDynaActionForm() where
> >>>>
> >>>>
> >dynaForm="
> >
> >
> >>>>+
> >>>>                         dynaForm + " and formBeanConfig=" +
> >>>>formBeanConfig);
> >>>>           }
> >>>>
> >>>>           initializeDynaForm(formName, dynaForm, moduleConfig);
> >>>>       } catch (Exception e) {
> >>>>           logger.error("Exception [" + e + "," + e.getMessage() +
"]",
> >>>>e);
> >>>>           // TODO what to do if we get an error initializing the
form?
> >>>>           // -> right now we're just returning null
> >>>>           dynaForm = null;
> >>>>       }
> >>>>       return dynaForm;
> >>>>   }
> >>>>
> >>>>   /**
> >>>>    * <p>Initialize all bean properties to their initial values, as
> >>>>specified
> >>>>    * in the {@link FormPropertyConfig} elements associated with the
> >>>>    * definition of this <code>DynaActionForm</code>.  Based on the
> >>>>    * <code>DynaActionForm.initialize()</code> method.
> >>>>    * </p>
> >>>>    *
> >>>>    * @param dynaForm the DynaActionForm to be initialized
> >>>>    * @param moduleConfig the ModuleConfig under which this dynaForm
> >>>>    *
> >>>>    */
> >>>>   protected static void initializeDynaForm(String name,
> >>>>                                            DynaActionForm dynaForm,
> >>>>                                            ModuleConfig moduleConfig)
> >>>>
> >>>>
> >{
> >
> >
> >>>>       FormBeanConfig config = moduleConfig.findFormBeanConfig(name);
> >>>>       if (config == null) {
> >>>>           return;
> >>>>       }
> >>>>       FormPropertyConfig props[] = config.findFormPropertyConfigs();
> >>>>       for (int i = 0; i < props.length; i++) {
> >>>>           dynaForm.set(props[i].getName(), props[i].initial());
> >>>>       }
> >>>>   }
> >>>>
> >>>>hth,
> >>>>Hubert
> >>>>
> >>>>--- Danko Desancic <dd...@northernobjects.com> wrote:
> >>>>
> >>>>
> >>>>
> >>>>
> >>>>>I am newbie to Struts and have a problem with  populating
> >>>>>DynaActionForm. I have two actions: one to set up userForm and one to
> >>>>>process it. 1st one is suposed to grab user info and populate the
form.
> >>>>>The second one updates user info after the form is submitted. The
> >>>>>problem is that the from is not populated. Moreover changing the
scope
> >>>>>to "session" in action "/user/setUp"  results in form being populated
> >>>>>with appropriate data. What am I doing wrong in here?
> >>>>>
> >>>>>Thanks
> >>>>>Danko
> >>>>>
> >>>>>
> >>>>>
> >>>>>from struts-config.xml
> >>>>
> >>>>
> >>>>><form-bean name="userForm"
> >>>>>type="org.apache.struts.action.DynaActionForm">that
> >>>>>           <form-property name="userName" type="java.lang.String"/>
> >>>>>           <form-property name="password" type="java.lang.String"/>
> >>>>>           <form-property name="fullName" type="java.lang.String"/>
> >>>>>           <form-property name="email" type="java.lang.String"/>
> >>>>>           <form-property name="status" type="java.lang.Integer"
> >>>>>initial="0"/>
> >>>>></form-bean>
> >>>>>
> >>>>><action
> >>>>>           path="/user/setUp"
> >>>>>           type="myPackage.user.SetUpUserAction"
> >>>>>           name="userForm"
> >>>>>           scope="request"
> >>>>>           validate="false">
> >>>>>           <forward name="Success"
> >>>>>path="/WEB-INF/jsp/user/userForm.jsp"/>
> >>>>></action>
> >>>>>
> >>>>><action
> >>>>>           path="/user/update"
> >>>>>           type="myPackage.user.UpdateUserAction"
> >>>>>           name="userForm"
> >>>>>           scope="request"
> >>>>>           validate="true"
> >>>>>           input="/WEB-INF/jsp/user/userForm.jsp">
> >>>>>           <forward name="Success"  path="/user/edit.do"
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>>redirect="true"/>
> >>>>
> >>>>
> >>>>
> >>>>
> >>>>></action>
> >>>>>
> >>>>>
> >>>>>
> >>>>>from SetUpUserAction.execute()
> >>>>
> >>>>
> >>>>>           DynaActionForm userForm = (DynaActionForm)form;
> >>>>>
> >>>>>           userForm.set("userName",user.getUserName());
> >>>>>           userForm.set("password",user.getPassword());
> >>>>>           userForm.set("fullName",user.getFullName());
> >>>>>           userForm.set("email",user.getEmail());
> >>>>>           userForm.set("status",new Integer(1));
> >>>>>
> >>>>>           forward = mapping.findForward("Success");
> >>>>>           return forward;
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>>>from userForm.jsp
> >>>>
> >>>>
> >>>>><html:form action="/user/update.do">
> >>>>><table cellpadding="2" cellspacing="2" border="1" width="800px">
> >>>>>   <tr>
> >>>>>       <td class="fieldHeader" colspan="4">UPDATE USER</td>
> >>>>>   </tr>
> >>>>>   <tr>
> >>>>>       <td class="fieldName">Username</td>
> >>>>>       <td><html:text property="userName"/></td>
> >>>>>   </tr>
> >>>>>  ......
> >>>>>   <tr>
> >>>>>       <td class="fieldName">Status</td>
> >>>>>       <td><html:radio property="status" value="1"/>Active &nbsp
> >>>>><html:radio property="status" value="0"/>Inactive </td>
> >>>>>   </tr>
> >>>>>   <tr>
> >>>>>       <td colspan="4" class="fieldName" align="center">
> >>>>>           <html:submit value="UPDATE"/>
> >>>>>       </td>
> >>>>>   </tr>
> >>>>></table>
> >>>>></html:form>
> >>>>>
> >>>>>
> >>>>>---------------------------------------------------------------------
> >>>>>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> >>>>>For additional commands, e-mail: struts-user-help@jakarta.apache.org
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>>>
> >>>>__________________________________
> >>>>Do you Yahoo!?
> >>>>Yahoo! Search - Find what you’re looking for faster
> >>>>http://search.yahoo.com
> >>>>
> >>>>---------------------------------------------------------------------
> >>>>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> >>>>For additional commands, e-mail: struts-user-help@jakarta.apache.org
> >>>>
> >>>>
> >>>>
> >>>>
> >>>>
> >>>__________________________________
> >>>Do you Yahoo!?
> >>>Yahoo! Search - Find what you’re looking for faster
> >>>http://search.yahoo.com
> >>>
> >>>---------------------------------------------------------------------
> >>>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> >>>For additional commands, e-mail: struts-user-help@jakarta.apache.org
> >>>
> >>>
> >>>
> >>>
> >>>
> >>>
> >>---------------------------------------------------------------------
> >>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> >>For additional commands, e-mail: struts-user-help@jakarta.apache.org
> >>
> >>
> >>
> >>
> >
> >
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> >For additional commands, e-mail: struts-user-help@jakarta.apache.org
> >
> >
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: (pre)populating DynaActionForm

Posted by Danko Desancic <dd...@northernobjects.com>.
This is really funny, I did output the request scope on the userForm.jsp 
and it turns out that Struts puts userForm in request scope on its own 
with all appropriate data that I set. The question is then why aren't 
they displayed in the html tags?

Any ideas?

Danko
 

Niall Pemberton wrote:

>Every thing looks fine to me. Is this the actual struts-config.xml you are
>trying to use - because I did notice your "/user/update" has re-direct in
>the "Success" forward.  Obviously you say the problem is with the
>"/user/setUp" action but I ask because a re-direct would cause the kind of
>behaviour you describe.
>
>Niall
>
>----- Original Message ----- 
>From: "Danko Desancic" <dd...@northernobjects.com>
>To: "Struts Users Mailing List" <st...@jakarta.apache.org>
>Sent: Friday, March 12, 2004 8:26 PM
>Subject: Re: (pre)populating DynaActionForm
>
>
>  
>
>>I tried that and it does not work. No errors though just balnk form with
>>initial value.
>>
>>Thanks anyway
>>
>>Hubert Rabago wrote:
>>
>>    
>>
>>>My bad.  I didn't read your mail through; only saw the top portion.
>>>In your setupUserAction, after you prepopulate the form, set it as a
>>>      
>>>
>request
>  
>
>>>attribute:
>>>
>>>
>>>
>>>      
>>>
>>>>>from SetUpUserAction.execute()
>>>>        
>>>>
>>>>>           DynaActionForm userForm = (DynaActionForm)form;
>>>>>
>>>>>           userForm.set("userName",user.getUserName());
>>>>>           userForm.set("password",user.getPassword());
>>>>>           userForm.set("fullName",user.getFullName());
>>>>>           userForm.set("email",user.getEmail());
>>>>>           userForm.set("status",new Integer(1));
>>>>>
>>>>>
>>>>>          
>>>>>
>>>               request.setAttribute("userForm",userForm);
>>>
>>>
>>>      
>>>
>>>>>           forward = mapping.findForward("Success");
>>>>>           return forward;
>>>>>
>>>>>
>>>>>          
>>>>>
>>>It works when the form is in session scope because that will make the
>>>      
>>>
>whole
>  
>
>>>session deal with one form instance.
>>>
>>>hth,
>>>Hubert
>>>
>>>--- Hubert Rabago <ja...@yahoo.com> wrote:
>>>
>>>
>>>      
>>>
>>>>As far as I can tell, there really isn't any support for prepopulating a
>>>>DynaActionForm.  The ff is from
>>>>
>>>>
>>>>
>>>>        
>>>>
>>http://jakarta.apache.org/struts/userGuide/building_controller.html#dyna_ac
>>    
>>
>tion_form_classes
>  
>
>>>      
>>>
>>>>"DynaActionForms are not a drop-in replacement for ActionForms. If you
>>>>        
>>>>
>need
>  
>
>>>>to access ActionForm properties in your Action, you will need to use the
>>>>map-style accessor, like myForm.get("name"). If you actively use the
>>>>ActionForm object in your Action, then you may want to use conventional
>>>>ActionForms instead.
>>>>
>>>>"DynaActionForms cannot be instantiated using a no-argument constructor.
>>>>        
>>>>
>In
>  
>
>>>>order to simulate the extra properties, there is a lot of machinery
>>>>involved
>>>>in their construction. You must rely on Struts to instantiate a
>>>>DynaActionForm for you, via the ActionMapping."
>>>>
>>>>What I did was copy the code that RequestUtils uses to instantiate a
>>>>DynaActionForm, and put it in a method that I can call like this:
>>>>DynaActionForm dynaForm = createDynaActionForm("formName",
>>>>mapping.getModuleConfig());
>>>>
>>>>
>>>>
>>>>   /**
>>>>    * <p>Create the {@link DynaActionForm} instance identified by the
>>>>given
>>>>    * formName.</p>
>>>>    *
>>>>    * @param formName the name used to identify the form
>>>>    * @param moduleConfig the configuration for the current module
>>>>    * @return the DynaActionForm identified by formName
>>>>    */
>>>>   public static DynaActionForm createDynaActionForm(
>>>>           String formName,
>>>>           ModuleConfig moduleConfig) {
>>>>       DynaActionForm dynaForm;
>>>>       try {
>>>>           // the code here is based on [copied from? :) ]
>>>>           //  RequestUtils.createActionForm()
>>>>           FormBeanConfig formBeanConfig =
>>>>                   moduleConfig.findFormBeanConfig(formName);
>>>>
>>>>           DynaActionFormClass dynaClass =
>>>>                   DynaActionFormClass.createDynaActionFormClass(
>>>>                           formBeanConfig);
>>>>
>>>>           dynaForm = (DynaActionForm) dynaClass.newInstance();
>>>>
>>>>           if (logger.isTraceEnabled()) {
>>>>               logger.trace("inside findDynaActionForm() where
>>>>        
>>>>
>dynaForm="
>  
>
>>>>+
>>>>                         dynaForm + " and formBeanConfig=" +
>>>>formBeanConfig);
>>>>           }
>>>>
>>>>           initializeDynaForm(formName, dynaForm, moduleConfig);
>>>>       } catch (Exception e) {
>>>>           logger.error("Exception [" + e + "," + e.getMessage() + "]",
>>>>e);
>>>>           // TODO what to do if we get an error initializing the form?
>>>>           // -> right now we're just returning null
>>>>           dynaForm = null;
>>>>       }
>>>>       return dynaForm;
>>>>   }
>>>>
>>>>   /**
>>>>    * <p>Initialize all bean properties to their initial values, as
>>>>specified
>>>>    * in the {@link FormPropertyConfig} elements associated with the
>>>>    * definition of this <code>DynaActionForm</code>.  Based on the
>>>>    * <code>DynaActionForm.initialize()</code> method.
>>>>    * </p>
>>>>    *
>>>>    * @param dynaForm the DynaActionForm to be initialized
>>>>    * @param moduleConfig the ModuleConfig under which this dynaForm
>>>>    *
>>>>    */
>>>>   protected static void initializeDynaForm(String name,
>>>>                                            DynaActionForm dynaForm,
>>>>                                            ModuleConfig moduleConfig)
>>>>        
>>>>
>{
>  
>
>>>>       FormBeanConfig config = moduleConfig.findFormBeanConfig(name);
>>>>       if (config == null) {
>>>>           return;
>>>>       }
>>>>       FormPropertyConfig props[] = config.findFormPropertyConfigs();
>>>>       for (int i = 0; i < props.length; i++) {
>>>>           dynaForm.set(props[i].getName(), props[i].initial());
>>>>       }
>>>>   }
>>>>
>>>>hth,
>>>>Hubert
>>>>
>>>>--- Danko Desancic <dd...@northernobjects.com> wrote:
>>>>
>>>>
>>>>        
>>>>
>>>>>I am newbie to Struts and have a problem with  populating
>>>>>DynaActionForm. I have two actions: one to set up userForm and one to
>>>>>process it. 1st one is suposed to grab user info and populate the form.
>>>>>The second one updates user info after the form is submitted. The
>>>>>problem is that the from is not populated. Moreover changing the scope
>>>>>to "session" in action "/user/setUp"  results in form being populated
>>>>>with appropriate data. What am I doing wrong in here?
>>>>>
>>>>>Thanks
>>>>>Danko
>>>>>
>>>>>          
>>>>>
>>>>>from struts-config.xml
>>>>        
>>>>
>>>>><form-bean name="userForm"
>>>>>type="org.apache.struts.action.DynaActionForm">that
>>>>>           <form-property name="userName" type="java.lang.String"/>
>>>>>           <form-property name="password" type="java.lang.String"/>
>>>>>           <form-property name="fullName" type="java.lang.String"/>
>>>>>           <form-property name="email" type="java.lang.String"/>
>>>>>           <form-property name="status" type="java.lang.Integer"
>>>>>initial="0"/>
>>>>></form-bean>
>>>>>
>>>>><action
>>>>>           path="/user/setUp"
>>>>>           type="myPackage.user.SetUpUserAction"
>>>>>           name="userForm"
>>>>>           scope="request"
>>>>>           validate="false">
>>>>>           <forward name="Success"
>>>>>path="/WEB-INF/jsp/user/userForm.jsp"/>
>>>>></action>
>>>>>
>>>>><action
>>>>>           path="/user/update"
>>>>>           type="myPackage.user.UpdateUserAction"
>>>>>           name="userForm"
>>>>>           scope="request"
>>>>>           validate="true"
>>>>>           input="/WEB-INF/jsp/user/userForm.jsp">
>>>>>           <forward name="Success"  path="/user/edit.do"
>>>>>
>>>>>
>>>>>          
>>>>>
>>>>redirect="true"/>
>>>>
>>>>
>>>>        
>>>>
>>>>></action>
>>>>>
>>>>>          
>>>>>
>>>>>from SetUpUserAction.execute()
>>>>        
>>>>
>>>>>           DynaActionForm userForm = (DynaActionForm)form;
>>>>>
>>>>>           userForm.set("userName",user.getUserName());
>>>>>           userForm.set("password",user.getPassword());
>>>>>           userForm.set("fullName",user.getFullName());
>>>>>           userForm.set("email",user.getEmail());
>>>>>           userForm.set("status",new Integer(1));
>>>>>
>>>>>           forward = mapping.findForward("Success");
>>>>>           return forward;
>>>>>
>>>>>
>>>>>          
>>>>>
>>>>>from userForm.jsp
>>>>        
>>>>
>>>>><html:form action="/user/update.do">
>>>>><table cellpadding="2" cellspacing="2" border="1" width="800px">
>>>>>   <tr>
>>>>>       <td class="fieldHeader" colspan="4">UPDATE USER</td>
>>>>>   </tr>
>>>>>   <tr>
>>>>>       <td class="fieldName">Username</td>
>>>>>       <td><html:text property="userName"/></td>
>>>>>   </tr>
>>>>>  ......
>>>>>   <tr>
>>>>>       <td class="fieldName">Status</td>
>>>>>       <td><html:radio property="status" value="1"/>Active &nbsp
>>>>><html:radio property="status" value="0"/>Inactive </td>
>>>>>   </tr>
>>>>>   <tr>
>>>>>       <td colspan="4" class="fieldName" align="center">
>>>>>           <html:submit value="UPDATE"/>
>>>>>       </td>
>>>>>   </tr>
>>>>></table>
>>>>></html:form>
>>>>>
>>>>>
>>>>>---------------------------------------------------------------------
>>>>>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>>>>>For additional commands, e-mail: struts-user-help@jakarta.apache.org
>>>>>
>>>>>
>>>>>
>>>>>          
>>>>>
>>>>__________________________________
>>>>Do you Yahoo!?
>>>>Yahoo! Search - Find what you’re looking for faster
>>>>http://search.yahoo.com
>>>>
>>>>---------------------------------------------------------------------
>>>>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>>>>For additional commands, e-mail: struts-user-help@jakarta.apache.org
>>>>
>>>>
>>>>
>>>>        
>>>>
>>>__________________________________
>>>Do you Yahoo!?
>>>Yahoo! Search - Find what you’re looking for faster
>>>http://search.yahoo.com
>>>
>>>---------------------------------------------------------------------
>>>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>>>For additional commands, e-mail: struts-user-help@jakarta.apache.org
>>>
>>>
>>>
>>>
>>>      
>>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: struts-user-help@jakarta.apache.org
>>
>>
>>    
>>
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>
>  
>


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: (pre)populating DynaActionForm

Posted by Niall Pemberton <ni...@blueyonder.co.uk>.
Every thing looks fine to me. Is this the actual struts-config.xml you are
trying to use - because I did notice your "/user/update" has re-direct in
the "Success" forward.  Obviously you say the problem is with the
"/user/setUp" action but I ask because a re-direct would cause the kind of
behaviour you describe.

Niall

----- Original Message ----- 
From: "Danko Desancic" <dd...@northernobjects.com>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>
Sent: Friday, March 12, 2004 8:26 PM
Subject: Re: (pre)populating DynaActionForm


> I tried that and it does not work. No errors though just balnk form with
> initial value.
>
> Thanks anyway
>
> Hubert Rabago wrote:
>
> >My bad.  I didn't read your mail through; only saw the top portion.
> >In your setupUserAction, after you prepopulate the form, set it as a
request
> >attribute:
> >
> >
> >
> >>>from SetUpUserAction.execute()
> >>>            DynaActionForm userForm = (DynaActionForm)form;
> >>>
> >>>            userForm.set("userName",user.getUserName());
> >>>            userForm.set("password",user.getPassword());
> >>>            userForm.set("fullName",user.getFullName());
> >>>            userForm.set("email",user.getEmail());
> >>>            userForm.set("status",new Integer(1));
> >>>
> >>>
> >                request.setAttribute("userForm",userForm);
> >
> >
> >>>
> >>>            forward = mapping.findForward("Success");
> >>>            return forward;
> >>>
> >>>
> >
> >It works when the form is in session scope because that will make the
whole
> >session deal with one form instance.
> >
> >hth,
> >Hubert
> >
> >--- Hubert Rabago <ja...@yahoo.com> wrote:
> >
> >
> >>As far as I can tell, there really isn't any support for prepopulating a
> >>DynaActionForm.  The ff is from
> >>
> >>
> >>
>
>http://jakarta.apache.org/struts/userGuide/building_controller.html#dyna_ac
tion_form_classes
> >
> >
> >>"DynaActionForms are not a drop-in replacement for ActionForms. If you
need
> >>to access ActionForm properties in your Action, you will need to use the
> >>map-style accessor, like myForm.get("name"). If you actively use the
> >>ActionForm object in your Action, then you may want to use conventional
> >>ActionForms instead.
> >>
> >>"DynaActionForms cannot be instantiated using a no-argument constructor.
In
> >>order to simulate the extra properties, there is a lot of machinery
> >>involved
> >>in their construction. You must rely on Struts to instantiate a
> >>DynaActionForm for you, via the ActionMapping."
> >>
> >>What I did was copy the code that RequestUtils uses to instantiate a
> >>DynaActionForm, and put it in a method that I can call like this:
> >>DynaActionForm dynaForm = createDynaActionForm("formName",
> >>mapping.getModuleConfig());
> >>
> >>
> >>
> >>    /**
> >>     * <p>Create the {@link DynaActionForm} instance identified by the
> >>given
> >>     * formName.</p>
> >>     *
> >>     * @param formName the name used to identify the form
> >>     * @param moduleConfig the configuration for the current module
> >>     * @return the DynaActionForm identified by formName
> >>     */
> >>    public static DynaActionForm createDynaActionForm(
> >>            String formName,
> >>            ModuleConfig moduleConfig) {
> >>        DynaActionForm dynaForm;
> >>        try {
> >>            // the code here is based on [copied from? :) ]
> >>            //  RequestUtils.createActionForm()
> >>            FormBeanConfig formBeanConfig =
> >>                    moduleConfig.findFormBeanConfig(formName);
> >>
> >>            DynaActionFormClass dynaClass =
> >>                    DynaActionFormClass.createDynaActionFormClass(
> >>                            formBeanConfig);
> >>
> >>            dynaForm = (DynaActionForm) dynaClass.newInstance();
> >>
> >>            if (logger.isTraceEnabled()) {
> >>                logger.trace("inside findDynaActionForm() where
dynaForm="
> >>+
> >>                          dynaForm + " and formBeanConfig=" +
> >>formBeanConfig);
> >>            }
> >>
> >>            initializeDynaForm(formName, dynaForm, moduleConfig);
> >>        } catch (Exception e) {
> >>            logger.error("Exception [" + e + "," + e.getMessage() + "]",
> >>e);
> >>            // TODO what to do if we get an error initializing the form?
> >>            // -> right now we're just returning null
> >>            dynaForm = null;
> >>        }
> >>        return dynaForm;
> >>    }
> >>
> >>    /**
> >>     * <p>Initialize all bean properties to their initial values, as
> >>specified
> >>     * in the {@link FormPropertyConfig} elements associated with the
> >>     * definition of this <code>DynaActionForm</code>.  Based on the
> >>     * <code>DynaActionForm.initialize()</code> method.
> >>     * </p>
> >>     *
> >>     * @param dynaForm the DynaActionForm to be initialized
> >>     * @param moduleConfig the ModuleConfig under which this dynaForm
> >>     *
> >>     */
> >>    protected static void initializeDynaForm(String name,
> >>                                             DynaActionForm dynaForm,
> >>                                             ModuleConfig moduleConfig)
{
> >>
> >>        FormBeanConfig config = moduleConfig.findFormBeanConfig(name);
> >>        if (config == null) {
> >>            return;
> >>        }
> >>        FormPropertyConfig props[] = config.findFormPropertyConfigs();
> >>        for (int i = 0; i < props.length; i++) {
> >>            dynaForm.set(props[i].getName(), props[i].initial());
> >>        }
> >>    }
> >>
> >>hth,
> >>Hubert
> >>
> >>--- Danko Desancic <dd...@northernobjects.com> wrote:
> >>
> >>
> >>>I am newbie to Struts and have a problem with  populating
> >>>DynaActionForm. I have two actions: one to set up userForm and one to
> >>>process it. 1st one is suposed to grab user info and populate the form.
> >>>The second one updates user info after the form is submitted. The
> >>>problem is that the from is not populated. Moreover changing the scope
> >>>to "session" in action "/user/setUp"  results in form being populated
> >>>with appropriate data. What am I doing wrong in here?
> >>>
> >>>Thanks
> >>>Danko
> >>>
> >>>from struts-config.xml
> >>><form-bean name="userForm"
> >>>type="org.apache.struts.action.DynaActionForm">that
> >>>            <form-property name="userName" type="java.lang.String"/>
> >>>            <form-property name="password" type="java.lang.String"/>
> >>>            <form-property name="fullName" type="java.lang.String"/>
> >>>            <form-property name="email" type="java.lang.String"/>
> >>>            <form-property name="status" type="java.lang.Integer"
> >>>initial="0"/>
> >>></form-bean>
> >>>
> >>><action
> >>>            path="/user/setUp"
> >>>            type="myPackage.user.SetUpUserAction"
> >>>            name="userForm"
> >>>            scope="request"
> >>>            validate="false">
> >>>            <forward name="Success"
> >>>path="/WEB-INF/jsp/user/userForm.jsp"/>
> >>></action>
> >>>
> >>><action
> >>>            path="/user/update"
> >>>            type="myPackage.user.UpdateUserAction"
> >>>            name="userForm"
> >>>            scope="request"
> >>>            validate="true"
> >>>            input="/WEB-INF/jsp/user/userForm.jsp">
> >>>            <forward name="Success"  path="/user/edit.do"
> >>>
> >>>
> >>redirect="true"/>
> >>
> >>
> >>> </action>
> >>>
> >>>from SetUpUserAction.execute()
> >>>            DynaActionForm userForm = (DynaActionForm)form;
> >>>
> >>>            userForm.set("userName",user.getUserName());
> >>>            userForm.set("password",user.getPassword());
> >>>            userForm.set("fullName",user.getFullName());
> >>>            userForm.set("email",user.getEmail());
> >>>            userForm.set("status",new Integer(1));
> >>>
> >>>            forward = mapping.findForward("Success");
> >>>            return forward;
> >>>
> >>>
> >>>from userForm.jsp
> >>><html:form action="/user/update.do">
> >>><table cellpadding="2" cellspacing="2" border="1" width="800px">
> >>>    <tr>
> >>>        <td class="fieldHeader" colspan="4">UPDATE USER</td>
> >>>    </tr>
> >>>    <tr>
> >>>        <td class="fieldName">Username</td>
> >>>        <td><html:text property="userName"/></td>
> >>>    </tr>
> >>>   ......
> >>>    <tr>
> >>>        <td class="fieldName">Status</td>
> >>>        <td><html:radio property="status" value="1"/>Active &nbsp
> >>><html:radio property="status" value="0"/>Inactive </td>
> >>>    </tr>
> >>>    <tr>
> >>>        <td colspan="4" class="fieldName" align="center">
> >>>            <html:submit value="UPDATE"/>
> >>>        </td>
> >>>    </tr>
> >>></table>
> >>></html:form>
> >>>
> >>>
> >>>---------------------------------------------------------------------
> >>>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> >>>For additional commands, e-mail: struts-user-help@jakarta.apache.org
> >>>
> >>>
> >>>
> >>__________________________________
> >>Do you Yahoo!?
> >>Yahoo! Search - Find what you’re looking for faster
> >>http://search.yahoo.com
> >>
> >>---------------------------------------------------------------------
> >>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> >>For additional commands, e-mail: struts-user-help@jakarta.apache.org
> >>
> >>
> >>
> >
> >
> >__________________________________
> >Do you Yahoo!?
> >Yahoo! Search - Find what you’re looking for faster
> >http://search.yahoo.com
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> >For additional commands, e-mail: struts-user-help@jakarta.apache.org
> >
> >
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: (pre)populating DynaActionForm

Posted by Danko Desancic <dd...@northernobjects.com>.
I tried that and it does not work. No errors though just balnk form with 
initial value.

Thanks anyway

Hubert Rabago wrote:

>My bad.  I didn't read your mail through; only saw the top portion.  
>In your setupUserAction, after you prepopulate the form, set it as a request
>attribute:
>
>  
>
>>>from SetUpUserAction.execute()
>>>            DynaActionForm userForm = (DynaActionForm)form;
>>>           
>>>            userForm.set("userName",user.getUserName());
>>>            userForm.set("password",user.getPassword());
>>>            userForm.set("fullName",user.getFullName());
>>>            userForm.set("email",user.getEmail());
>>>            userForm.set("status",new Integer(1));
>>>      
>>>
>                request.setAttribute("userForm",userForm);
>  
>
>>>                      
>>>            forward = mapping.findForward("Success");
>>>            return forward;
>>>      
>>>
>
>It works when the form is in session scope because that will make the whole
>session deal with one form instance.
>
>hth,
>Hubert
>
>--- Hubert Rabago <ja...@yahoo.com> wrote:
>  
>
>>As far as I can tell, there really isn't any support for prepopulating a
>>DynaActionForm.  The ff is from
>>
>>    
>>
>http://jakarta.apache.org/struts/userGuide/building_controller.html#dyna_action_form_classes
>  
>
>>"DynaActionForms are not a drop-in replacement for ActionForms. If you need
>>to access ActionForm properties in your Action, you will need to use the
>>map-style accessor, like myForm.get("name"). If you actively use the
>>ActionForm object in your Action, then you may want to use conventional
>>ActionForms instead. 
>>
>>"DynaActionForms cannot be instantiated using a no-argument constructor. In
>>order to simulate the extra properties, there is a lot of machinery
>>involved
>>in their construction. You must rely on Struts to instantiate a
>>DynaActionForm for you, via the ActionMapping."
>>
>>What I did was copy the code that RequestUtils uses to instantiate a
>>DynaActionForm, and put it in a method that I can call like this:
>>DynaActionForm dynaForm = createDynaActionForm("formName",
>>mapping.getModuleConfig());
>>
>>
>>
>>    /**
>>     * <p>Create the {@link DynaActionForm} instance identified by the
>>given
>>     * formName.</p>
>>     * 
>>     * @param formName the name used to identify the form
>>     * @param moduleConfig the configuration for the current module
>>     * @return the DynaActionForm identified by formName
>>     */
>>    public static DynaActionForm createDynaActionForm(
>>            String formName,
>>            ModuleConfig moduleConfig) {
>>        DynaActionForm dynaForm;
>>        try {
>>            // the code here is based on [copied from? :) ]
>>            //  RequestUtils.createActionForm()
>>            FormBeanConfig formBeanConfig =
>>                    moduleConfig.findFormBeanConfig(formName);
>>
>>            DynaActionFormClass dynaClass =
>>                    DynaActionFormClass.createDynaActionFormClass(
>>                            formBeanConfig);
>>
>>            dynaForm = (DynaActionForm) dynaClass.newInstance();
>>
>>            if (logger.isTraceEnabled()) {
>>                logger.trace("inside findDynaActionForm() where dynaForm="
>>+
>>                          dynaForm + " and formBeanConfig=" +
>>formBeanConfig);
>>            }
>>
>>            initializeDynaForm(formName, dynaForm, moduleConfig);
>>        } catch (Exception e) {
>>            logger.error("Exception [" + e + "," + e.getMessage() + "]",
>>e);
>>            // TODO what to do if we get an error initializing the form?
>>            // -> right now we're just returning null
>>            dynaForm = null;
>>        }
>>        return dynaForm;
>>    }
>>
>>    /**
>>     * <p>Initialize all bean properties to their initial values, as
>>specified
>>     * in the {@link FormPropertyConfig} elements associated with the
>>     * definition of this <code>DynaActionForm</code>.  Based on the
>>     * <code>DynaActionForm.initialize()</code> method.
>>     * </p>
>>     *
>>     * @param dynaForm the DynaActionForm to be initialized
>>     * @param moduleConfig the ModuleConfig under which this dynaForm
>>     *
>>     */
>>    protected static void initializeDynaForm(String name,
>>                                             DynaActionForm dynaForm,
>>                                             ModuleConfig moduleConfig) {
>>
>>        FormBeanConfig config = moduleConfig.findFormBeanConfig(name);
>>        if (config == null) {
>>            return;
>>        }
>>        FormPropertyConfig props[] = config.findFormPropertyConfigs();
>>        for (int i = 0; i < props.length; i++) {
>>            dynaForm.set(props[i].getName(), props[i].initial());
>>        }
>>    }
>>
>>hth,
>>Hubert
>>
>>--- Danko Desancic <dd...@northernobjects.com> wrote:
>>    
>>
>>>I am newbie to Struts and have a problem with  populating 
>>>DynaActionForm. I have two actions: one to set up userForm and one to 
>>>process it. 1st one is suposed to grab user info and populate the form. 
>>>The second one updates user info after the form is submitted. The 
>>>problem is that the from is not populated. Moreover changing the scope 
>>>to "session" in action "/user/setUp"  results in form being populated 
>>>with appropriate data. What am I doing wrong in here?
>>>
>>>Thanks
>>>Danko
>>>
>>>from struts-config.xml
>>><form-bean name="userForm" 
>>>type="org.apache.struts.action.DynaActionForm">that
>>>            <form-property name="userName" type="java.lang.String"/>
>>>            <form-property name="password" type="java.lang.String"/>
>>>            <form-property name="fullName" type="java.lang.String"/>
>>>            <form-property name="email" type="java.lang.String"/>
>>>            <form-property name="status" type="java.lang.Integer" 
>>>initial="0"/>
>>></form-bean>
>>>
>>><action
>>>            path="/user/setUp"
>>>            type="myPackage.user.SetUpUserAction"
>>>            name="userForm"
>>>            scope="request"
>>>            validate="false">
>>>            <forward name="Success"  
>>>path="/WEB-INF/jsp/user/userForm.jsp"/>
>>></action>
>>>       
>>><action
>>>            path="/user/update"
>>>            type="myPackage.user.UpdateUserAction"
>>>            name="userForm"
>>>            scope="request"
>>>            validate="true"
>>>            input="/WEB-INF/jsp/user/userForm.jsp">
>>>            <forward name="Success"  path="/user/edit.do"
>>>      
>>>
>>redirect="true"/>
>>    
>>
>>> </action>
>>>
>>>from SetUpUserAction.execute()
>>>            DynaActionForm userForm = (DynaActionForm)form;
>>>           
>>>            userForm.set("userName",user.getUserName());
>>>            userForm.set("password",user.getPassword());
>>>            userForm.set("fullName",user.getFullName());
>>>            userForm.set("email",user.getEmail());
>>>            userForm.set("status",new Integer(1));
>>>                      
>>>            forward = mapping.findForward("Success");
>>>            return forward;
>>>
>>>
>>>from userForm.jsp
>>><html:form action="/user/update.do">
>>><table cellpadding="2" cellspacing="2" border="1" width="800px">
>>>    <tr>
>>>        <td class="fieldHeader" colspan="4">UPDATE USER</td>
>>>    </tr>
>>>    <tr>
>>>        <td class="fieldName">Username</td>
>>>        <td><html:text property="userName"/></td>
>>>    </tr>
>>>   ......
>>>    <tr>
>>>        <td class="fieldName">Status</td>
>>>        <td><html:radio property="status" value="1"/>Active &nbsp 
>>><html:radio property="status" value="0"/>Inactive </td>
>>>    </tr>
>>>    <tr>
>>>        <td colspan="4" class="fieldName" align="center">
>>>            <html:submit value="UPDATE"/>
>>>        </td>
>>>    </tr>
>>></table>
>>></html:form>
>>>
>>>
>>>---------------------------------------------------------------------
>>>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>>>For additional commands, e-mail: struts-user-help@jakarta.apache.org
>>>
>>>      
>>>
>>__________________________________
>>Do you Yahoo!?
>>Yahoo! Search - Find what you’re looking for faster
>>http://search.yahoo.com
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: struts-user-help@jakarta.apache.org
>>
>>    
>>
>
>
>__________________________________
>Do you Yahoo!?
>Yahoo! Search - Find what you’re looking for faster
>http://search.yahoo.com
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: struts-user-help@jakarta.apache.org
>
>
>  
>


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: (pre)populating DynaActionForm

Posted by Hubert Rabago <ja...@yahoo.com>.
My bad.  I didn't read your mail through; only saw the top portion.  
In your setupUserAction, after you prepopulate the form, set it as a request
attribute:

> > from SetUpUserAction.execute()
> >             DynaActionForm userForm = (DynaActionForm)form;
> >            
> >             userForm.set("userName",user.getUserName());
> >             userForm.set("password",user.getPassword());
> >             userForm.set("fullName",user.getFullName());
> >             userForm.set("email",user.getEmail());
> >             userForm.set("status",new Integer(1));
                request.setAttribute("userForm",userForm);
> >                       
> >             forward = mapping.findForward("Success");
> >             return forward;

It works when the form is in session scope because that will make the whole
session deal with one form instance.

hth,
Hubert

--- Hubert Rabago <ja...@yahoo.com> wrote:
> As far as I can tell, there really isn't any support for prepopulating a
> DynaActionForm.  The ff is from
>
http://jakarta.apache.org/struts/userGuide/building_controller.html#dyna_action_form_classes
> 
> "DynaActionForms are not a drop-in replacement for ActionForms. If you need
> to access ActionForm properties in your Action, you will need to use the
> map-style accessor, like myForm.get("name"). If you actively use the
> ActionForm object in your Action, then you may want to use conventional
> ActionForms instead. 
> 
> "DynaActionForms cannot be instantiated using a no-argument constructor. In
> order to simulate the extra properties, there is a lot of machinery
> involved
> in their construction. You must rely on Struts to instantiate a
> DynaActionForm for you, via the ActionMapping."
> 
> What I did was copy the code that RequestUtils uses to instantiate a
> DynaActionForm, and put it in a method that I can call like this:
> DynaActionForm dynaForm = createDynaActionForm("formName",
> mapping.getModuleConfig());
> 
> 
> 
>     /**
>      * <p>Create the {@link DynaActionForm} instance identified by the
> given
>      * formName.</p>
>      * 
>      * @param formName the name used to identify the form
>      * @param moduleConfig the configuration for the current module
>      * @return the DynaActionForm identified by formName
>      */
>     public static DynaActionForm createDynaActionForm(
>             String formName,
>             ModuleConfig moduleConfig) {
>         DynaActionForm dynaForm;
>         try {
>             // the code here is based on [copied from? :) ]
>             //  RequestUtils.createActionForm()
>             FormBeanConfig formBeanConfig =
>                     moduleConfig.findFormBeanConfig(formName);
> 
>             DynaActionFormClass dynaClass =
>                     DynaActionFormClass.createDynaActionFormClass(
>                             formBeanConfig);
> 
>             dynaForm = (DynaActionForm) dynaClass.newInstance();
> 
>             if (logger.isTraceEnabled()) {
>                 logger.trace("inside findDynaActionForm() where dynaForm="
> +
>                           dynaForm + " and formBeanConfig=" +
> formBeanConfig);
>             }
> 
>             initializeDynaForm(formName, dynaForm, moduleConfig);
>         } catch (Exception e) {
>             logger.error("Exception [" + e + "," + e.getMessage() + "]",
> e);
>             // TODO what to do if we get an error initializing the form?
>             // -> right now we're just returning null
>             dynaForm = null;
>         }
>         return dynaForm;
>     }
> 
>     /**
>      * <p>Initialize all bean properties to their initial values, as
> specified
>      * in the {@link FormPropertyConfig} elements associated with the
>      * definition of this <code>DynaActionForm</code>.  Based on the
>      * <code>DynaActionForm.initialize()</code> method.
>      * </p>
>      *
>      * @param dynaForm the DynaActionForm to be initialized
>      * @param moduleConfig the ModuleConfig under which this dynaForm
>      *
>      */
>     protected static void initializeDynaForm(String name,
>                                              DynaActionForm dynaForm,
>                                              ModuleConfig moduleConfig) {
> 
>         FormBeanConfig config = moduleConfig.findFormBeanConfig(name);
>         if (config == null) {
>             return;
>         }
>         FormPropertyConfig props[] = config.findFormPropertyConfigs();
>         for (int i = 0; i < props.length; i++) {
>             dynaForm.set(props[i].getName(), props[i].initial());
>         }
>     }
> 
> hth,
> Hubert
> 
> --- Danko Desancic <dd...@northernobjects.com> wrote:
> > I am newbie to Struts and have a problem with  populating 
> > DynaActionForm. I have two actions: one to set up userForm and one to 
> > process it. 1st one is suposed to grab user info and populate the form. 
> > The second one updates user info after the form is submitted. The 
> > problem is that the from is not populated. Moreover changing the scope 
> > to "session" in action "/user/setUp"  results in form being populated 
> > with appropriate data. What am I doing wrong in here?
> > 
> > Thanks
> > Danko
> > 
> > from struts-config.xml
> > <form-bean name="userForm" 
> > type="org.apache.struts.action.DynaActionForm">that
> >             <form-property name="userName" type="java.lang.String"/>
> >             <form-property name="password" type="java.lang.String"/>
> >             <form-property name="fullName" type="java.lang.String"/>
> >             <form-property name="email" type="java.lang.String"/>
> >             <form-property name="status" type="java.lang.Integer" 
> > initial="0"/>
> > </form-bean>
> > 
> > <action
> >             path="/user/setUp"
> >             type="myPackage.user.SetUpUserAction"
> >             name="userForm"
> >             scope="request"
> >             validate="false">
> >             <forward name="Success"  
> > path="/WEB-INF/jsp/user/userForm.jsp"/>
> > </action>
> >        
> > <action
> >             path="/user/update"
> >             type="myPackage.user.UpdateUserAction"
> >             name="userForm"
> >             scope="request"
> >             validate="true"
> >             input="/WEB-INF/jsp/user/userForm.jsp">
> >             <forward name="Success"  path="/user/edit.do"
> redirect="true"/>
> >  </action>
> > 
> > from SetUpUserAction.execute()
> >             DynaActionForm userForm = (DynaActionForm)form;
> >            
> >             userForm.set("userName",user.getUserName());
> >             userForm.set("password",user.getPassword());
> >             userForm.set("fullName",user.getFullName());
> >             userForm.set("email",user.getEmail());
> >             userForm.set("status",new Integer(1));
> >                       
> >             forward = mapping.findForward("Success");
> >             return forward;
> > 
> > 
> > from userForm.jsp
> > <html:form action="/user/update.do">
> > <table cellpadding="2" cellspacing="2" border="1" width="800px">
> >     <tr>
> >         <td class="fieldHeader" colspan="4">UPDATE USER</td>
> >     </tr>
> >     <tr>
> >         <td class="fieldName">Username</td>
> >         <td><html:text property="userName"/></td>
> >     </tr>
> >    ......
> >     <tr>
> >         <td class="fieldName">Status</td>
> >         <td><html:radio property="status" value="1"/>Active &nbsp 
> > <html:radio property="status" value="0"/>Inactive </td>
> >     </tr>
> >     <tr>
> >         <td colspan="4" class="fieldName" align="center">
> >             <html:submit value="UPDATE"/>
> >         </td>
> >     </tr>
> > </table>
> > </html:form>
> > 
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: struts-user-help@jakarta.apache.org
> > 
> 
> 
> __________________________________
> Do you Yahoo!?
> Yahoo! Search - Find what you�re looking for faster
> http://search.yahoo.com
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 


__________________________________
Do you Yahoo!?
Yahoo! Search - Find what you�re looking for faster
http://search.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: (pre)populating DynaActionForm

Posted by Hubert Rabago <ja...@yahoo.com>.
As far as I can tell, there really isn't any support for prepopulating a
DynaActionForm.  The ff is from
http://jakarta.apache.org/struts/userGuide/building_controller.html#dyna_action_form_classes

"DynaActionForms are not a drop-in replacement for ActionForms. If you need
to access ActionForm properties in your Action, you will need to use the
map-style accessor, like myForm.get("name"). If you actively use the
ActionForm object in your Action, then you may want to use conventional
ActionForms instead. 

"DynaActionForms cannot be instantiated using a no-argument constructor. In
order to simulate the extra properties, there is a lot of machinery involved
in their construction. You must rely on Struts to instantiate a
DynaActionForm for you, via the ActionMapping."

What I did was copy the code that RequestUtils uses to instantiate a
DynaActionForm, and put it in a method that I can call like this:
DynaActionForm dynaForm = createDynaActionForm("formName",
mapping.getModuleConfig());



    /**
     * <p>Create the {@link DynaActionForm} instance identified by the given
     * formName.</p>
     * 
     * @param formName the name used to identify the form
     * @param moduleConfig the configuration for the current module
     * @return the DynaActionForm identified by formName
     */
    public static DynaActionForm createDynaActionForm(
            String formName,
            ModuleConfig moduleConfig) {
        DynaActionForm dynaForm;
        try {
            // the code here is based on [copied from? :) ]
            //  RequestUtils.createActionForm()
            FormBeanConfig formBeanConfig =
                    moduleConfig.findFormBeanConfig(formName);

            DynaActionFormClass dynaClass =
                    DynaActionFormClass.createDynaActionFormClass(
                            formBeanConfig);

            dynaForm = (DynaActionForm) dynaClass.newInstance();

            if (logger.isTraceEnabled()) {
                logger.trace("inside findDynaActionForm() where dynaForm=" +
                          dynaForm + " and formBeanConfig=" +
formBeanConfig);
            }

            initializeDynaForm(formName, dynaForm, moduleConfig);
        } catch (Exception e) {
            logger.error("Exception [" + e + "," + e.getMessage() + "]", e);
            // TODO what to do if we get an error initializing the form?
            // -> right now we're just returning null
            dynaForm = null;
        }
        return dynaForm;
    }

    /**
     * <p>Initialize all bean properties to their initial values, as
specified
     * in the {@link FormPropertyConfig} elements associated with the
     * definition of this <code>DynaActionForm</code>.  Based on the
     * <code>DynaActionForm.initialize()</code> method.
     * </p>
     *
     * @param dynaForm the DynaActionForm to be initialized
     * @param moduleConfig the ModuleConfig under which this dynaForm
     *
     */
    protected static void initializeDynaForm(String name,
                                             DynaActionForm dynaForm,
                                             ModuleConfig moduleConfig) {

        FormBeanConfig config = moduleConfig.findFormBeanConfig(name);
        if (config == null) {
            return;
        }
        FormPropertyConfig props[] = config.findFormPropertyConfigs();
        for (int i = 0; i < props.length; i++) {
            dynaForm.set(props[i].getName(), props[i].initial());
        }
    }

hth,
Hubert

--- Danko Desancic <dd...@northernobjects.com> wrote:
> I am newbie to Struts and have a problem with  populating 
> DynaActionForm. I have two actions: one to set up userForm and one to 
> process it. 1st one is suposed to grab user info and populate the form. 
> The second one updates user info after the form is submitted. The 
> problem is that the from is not populated. Moreover changing the scope 
> to "session" in action "/user/setUp"  results in form being populated 
> with appropriate data. What am I doing wrong in here?
> 
> Thanks
> Danko
> 
> from struts-config.xml
> <form-bean name="userForm" 
> type="org.apache.struts.action.DynaActionForm">that
>             <form-property name="userName" type="java.lang.String"/>
>             <form-property name="password" type="java.lang.String"/>
>             <form-property name="fullName" type="java.lang.String"/>
>             <form-property name="email" type="java.lang.String"/>
>             <form-property name="status" type="java.lang.Integer" 
> initial="0"/>
> </form-bean>
> 
> <action
>             path="/user/setUp"
>             type="myPackage.user.SetUpUserAction"
>             name="userForm"
>             scope="request"
>             validate="false">
>             <forward name="Success"  
> path="/WEB-INF/jsp/user/userForm.jsp"/>
> </action>
>        
> <action
>             path="/user/update"
>             type="myPackage.user.UpdateUserAction"
>             name="userForm"
>             scope="request"
>             validate="true"
>             input="/WEB-INF/jsp/user/userForm.jsp">
>             <forward name="Success"  path="/user/edit.do" redirect="true"/>
>  </action>
> 
> from SetUpUserAction.execute()
>             DynaActionForm userForm = (DynaActionForm)form;
>            
>             userForm.set("userName",user.getUserName());
>             userForm.set("password",user.getPassword());
>             userForm.set("fullName",user.getFullName());
>             userForm.set("email",user.getEmail());
>             userForm.set("status",new Integer(1));
>                       
>             forward = mapping.findForward("Success");
>             return forward;
> 
> 
> from userForm.jsp
> <html:form action="/user/update.do">
> <table cellpadding="2" cellspacing="2" border="1" width="800px">
>     <tr>
>         <td class="fieldHeader" colspan="4">UPDATE USER</td>
>     </tr>
>     <tr>
>         <td class="fieldName">Username</td>
>         <td><html:text property="userName"/></td>
>     </tr>
>    ......
>     <tr>
>         <td class="fieldName">Status</td>
>         <td><html:radio property="status" value="1"/>Active &nbsp 
> <html:radio property="status" value="0"/>Inactive </td>
>     </tr>
>     <tr>
>         <td colspan="4" class="fieldName" align="center">
>             <html:submit value="UPDATE"/>
>         </td>
>     </tr>
> </table>
> </html:form>
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 


__________________________________
Do you Yahoo!?
Yahoo! Search - Find what you�re looking for faster
http://search.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org