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 [EMAIL PROTECTED] 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 [EMAIL PROTECTED] 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 <[EMAIL PROTECTED]> 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  
> <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: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
__________________________________
Do you Yahoo!?
Yahoo! Search - Find what you�re looking for faster
http://search.yahoo.com
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]