Hi Niall,

I figured out the problem. The problem was that, my DynaClass returned the class name for the getName() method call , whereas the DynaActionFormClass returns the name through getName() method of FormBeanConfig. This is the key that is used to store and retrieve the bean instance. As a result, in the createActionForm of RequestUtils, the if check(in bold ) will fail and the stored instance will not be returned.

if (config.getDynamic()) {
String className = ((DynaBean) instance).getDynaClass().getName();
* if (className.equals(config.getName())) {*
if (log.isDebugEnabled()) {
log.debug(
" Recycling existing DynaActionForm instance "
+ "of type '"
+ className
+ "'");
log.trace(" --> " + instance);
}
return (instance);
}


As my class is an extension of DynaActionForm, the following if check will be satisfied, and a new instance will be returned each and every time.

      if (config.getDynamic()) {
           try {
*                DynaActionFormClass dynaClass =
                   DynaActionFormClass.createDynaActionFormClass(config);
               instance = (ActionForm) dynaClass.newInstance();*
               ((DynaActionForm) instance).initialize(mapping);
               if (log.isDebugEnabled()) {
                   log.debug(
                       " Creating new DynaActionForm instance "
                           + "of type '"
                           + config.getType()
                           + "'");
                   log.trace(" --> " + instance);
               }
           }


I have rectified it by writing a separate implementation instead of extending the DynaActionForm, so that the else part of the first code snippet will be executed. Now everything is working fine. Thanks for your contribution without which i would be duplicating the informations in the jsp page and the xml file. Your idea has reduced a lot of coding. Thanks once again


Thanks
Shanmugam PL


Niall Pemberton wrote:


Shanmugam,

I think your problem lies in the way you have defined your two actions in the struts-config.xml - one of them has scope="session" and the other doesn't, which means it will probably default to request. I think this is causing your problem because Struts only looks for the ActionForm in the scope of the mapping specified - so in your case I believe its creating two versions of "severityForm". Try addding a scope="session" to your "/severityDetails" action and see if that sorts it out.

Niall

----- Original Message ----- From: shanmugampl To: Struts Users Mailing List Sent: Friday, March 12, 2004 3:55 AM
Subject: Re: Populating form Elements from another object.



Hi Niall,


Thanks for your inputs. I followed your methodology with slight changes. I have a class MCDynaClass which equals your LazyDynaClass. Instead of having a separate bean as LazyDynaBean, i extended the DynaActionForm as MCDynaActionForm and overridden the set methods as per my requirement. The entry in the struts-config.xml will be like

 <form-beans>
     <form-bean name = "severityForm" type = 
"com.adventnet.client.struts.action.MCDynaActionForm"/>
 </form-beans>

 <action path = "/severityDetails" type = 
"com.adventnet.client.struts.action.SeverityDetailsAction"   name = "severityForm" >
    <forward name = "details" path = "/jsp/detailsPage.jsp"/>
 </action>
 <action path = "/updateSeverity" type = "com.adventnet.client.struts.action.UpdateSeverityDetailsAction" name = 
"severityForm" input = "/severityDetails.do" scope = "session">
       <forward name = "success" path = "/jsp/results.jsp"/>
 </action>

In the ActionClass i will get the DataObject(my own object) and set it to the form through the setDataObject method.

 MCDynaActionForm userForm = (MCDynaActionForm) form;
 userForm.setDataObject(newDO);

By this way i am able to populate the values dynamically. The problems comes when i am submitting the form. When the form is submitted, the instance of the form that i am getting in the action class is different. As a result, I cannot update the dataObject as it is null. I could not figure out where i have gone wrong. I have attached my source. Kindly help me.

 Thanks
 Shanmugam PL



Niall Pemberton wrote:

Yup, thats it - plus dynamic="true"

<form-bean name="fooForm1"
type="lib.framework.struts.LazyValidatorActionForm" dynamic="true" />
<form-bean name="fooForm2"
type="lib.framework.struts.LazyValidatorActionForm" dynamic="true" />
<form-bean name="fooForm3"
type="lib.framework.struts.LazyValidatorActionForm" dynamic="true" />
<form-bean name="fooForm4"
type="lib.framework.struts.LazyValidatorActionForm" dynamic="true" />

Oh, I noticed an error in LazyValidatorForm, its declared as "abstract" -
which it shouldn't be - I don't use it directly, I use
LazyValidatorActionForm which extends it.

Niall


----- Original Message ----- From: "Mark Lowe" <[EMAIL PROTECTED]>
To: "Struts Users Mailing List" <[EMAIL PROTECTED]>
Sent: Tuesday, March 09, 2004 8:53 AM
Subject: Re: Populating form Elements from another object.



I haven't seen the code but if what i understand of what Niall has been saying you'd use them instead of DynaActionForm.

<form-bean name="fooForm" type="com.ilovesparrows.struts.NiallsForm" />

of course you'll need to call the package and class name to something
appropriate.

On 9 Mar 2004, at 09:47, shanmugampl wrote:

Hi,

  I saw your code. I have one doubt. How do you plugin your own
DynaBean implementation into the struts framework.

Shanmugam PL

Niall Pemberton wrote:

I wrote these....

http://www.niallp.pwp.blueyonder.co.uk

Niall

----- Original Message ----- From: "shanmugampl"
<[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, February 25, 2004 7:00 AM
Subject: Populating form Elements from another object.



Hi,

    I have a requirement where i need to populate the values of a
form from another object, and then again transform the contents of
the form back to the object once the form is submitted. i.e Say,
when i click a button, i do some functionalities and have a
Properties object as an output. The keys in the Properties object
are not static. These values need to be shown in a form. As the keys
retrieved for the current operation is not known, i cant define the
values in the struts-config.xml for the dynaactionclass.

     Is it possible to extend the dynaactionclass, and during its
init or something, iterate the properties object and populate the
form.  Are there any other way of doing it.

Thanks
Shanmugam PL


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



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


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





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



------------------------------------------------------------------------------


package com.adventnet.client.struts.action;


import org.apache.struts.action.DynaActionForm;

 import java.lang.reflect.Array;
 import java.util.*;
 import com.adventnet.persistence.ejb.*;
 import com.adventnet.persistence.*;
 import com.adventnet.nms.ms.config.client.ConfigurationCache;
 import org.apache.commons.beanutils.*;

public class MCDynaActionForm extends DynaActionForm {

protected MCDynaClass wccDynaClass = null;

public MCDynaActionForm(){
wccDynaClass = new MCDynaClass(); }


     public DynaClass getDynaClass() {
         return (this.wccDynaClass);
     }

public void set(String name, Object value) {
System.out.println("*********** SHAN : Set method called. Updation takes place in data object also ********** " + dataObject); System.out.println("*********** SHAN : HashValue is ********** " + hashCode()); ((MCDynaClass)getDynaClass()).add(name, value.getClass());
dynaValues.put(name, value);
if(dataObject != null){
String[] values = name.split(":");
String tableName = values[0];
String columnName = values[1];
try{
// This will update in all the rows that are present with this value.
dataObject.set(tableName, columnName, value);
}
catch(Exception e){
e.printStackTrace();
}
}
}



public void set(String name, int index, Object value) { ((MCDynaClass)getDynaClass()).add(name, value.getClass()); dynaValues.put(name, value); }


public void set(String name, String key, Object value) { ((MCDynaClass)getDynaClass()).add(name, value.getClass()); dynaValues.put(name, value); }

public DataObject dataObject = null;

 public void setDataObject(Object dataObject){
 this.dataObject = (DataObject) dataObject;
 setFormValues();
 }

 public Object getDataObject(){
 return dataObject;
 }

 public void setFormValues(){
 try{
 List tableList = dataObject.getTableNames();
 if(tableList != null && tableList.size() > 0){
 int size = tableList.size();
 for(int i = 0; i < size; i++){
 String tableName = (String) tableList.get(i);
 Row row = dataObject.getFirstRow(tableName);
 List columnNames = row.getColumns();
 List columnTypes = row.getColumnTypes();
 if(columnNames != null && columnNames.size() > 0){
 int count = columnNames.size();
 for(int j = 0; j < count; j++){
 String columnName = (String) columnNames.get(j);
 Object value = row.get(columnName);
 set(tableName + ":" + columnName, value);
 }
 }
 }
 }
 }
 catch(Exception e){
 e.printStackTrace();
 }
 }

 public DataObject createDataObject() throws DataAccessException{
 try{
 DataObject createdDO = null;
 PersistenceRemote persistenceAPI = ConfigurationCache.getPersistenceAPI();
 createdDO = persistenceAPI.constructDataObject();
 Iterator set = dynaValues.keySet().iterator();
 Properties rows = new Properties();
 Row row = null;
 while(set.hasNext()){
 String name = (String) set.next();
 Object value = dynaValues.get(name);
 String[] values = name.split(":");
 String tableName = values[0];
 String columnName = values[1];
 row = (Row) rows.get(tableName);
 if(row == null){
 row = new Row(tableName);
 rows.put(tableName, row);
 }
 row.set(columnName, value);
 }
 Enumeration enum = rows.propertyNames();
 while(enum.hasMoreElements()){
 String currentTableName = (String) enum.nextElement();
 Row currentRow = (Row) rows.get(currentTableName);
 createdDO.addRow(currentRow);
 }
 return createdDO;
 }
 catch(Exception e){
 e.printStackTrace();
 return null;
 }
 }
 }




------------------------------------------------------------------------------



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


Reply via email to