[ Disclaimer: Others may disagree - I'm new at this ;) ]

The best way is to assign one property to be a HashMap. 
There are limitations - but none that have really stood in
my apps way...


1. See MapBackedForm example in 4.3.2 Map-backed ActionForms
   of
http://jakarta.apache.org/struts/userGuide/building_controller.html#config. 

-  It is possible to refer to elements in the jsp, that do not (yet) have
even corresponding entries in the map... very cool... (see above)

2. On that note, a combination of DynaBean form and mapbackedform would
probably look like

<form-bean>
  ...
  <form-property
     name="value"
     type="java.util.HashMap" />
  ...
</form-bean>

!! HOWEVER !!
I had significant problems using the above for 
<html:text name="mydynabean" property="value(keyname)" />

beanUtils refused to populate the field - 
although it would render.

logic:iterate had a problem even when i dealt with each element
as a type="Map.MapEntry".

Craig, maybe it is not putting the Map.Entry back in place on a
populate?? I did have problems using :text and MapEntry - it wouldnt
populate to save it's life...

Anyway, I didn't get what I wanted from DynaBean.
So I've since just 'extended' ValidatorActionForm,
http://jakarta.apache.org/struts/api/org/apache/struts/validator/ValidatorAc
tionForm.html
which seems to work pretty well even with validating fields
of Map (truely dynamic) variables without problem, like this: 

package com.hp.sm.model;

import java.util.Map;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.commons.beanutils.PropertyUtils;

import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionErrors;

public class SmModelForm 
extends org.apache.struts.validator.ValidatorActionForm {

    private static Log log = LogFactory.getLog(SmModelForm.class);
    private static boolean debug = true;

    private final Map values = new HashMap();
    private final Map login = new HashMap();

    public void setValue(String key, Object value) {
        values.put(key, value);
    }

    public Object getValue(String key) {
        return values.get(key);
    }

    public ActionErrors validate( ActionMapping mapping,
      HttpServletRequest request) {

                // validate using the commons-validator and validation.xml
                ActionErrors errors = super.validate(mapping, request);

                // perform our additional validation here.
                if ( (errors==null) || (errors.isEmpty()) )
                  errors = SmModelHelper.validateSmModelForm( this, 
                                   request.getSession().getServletContext()
);

                if ( (errors==null) || (errors.isEmpty()) )
                  return null;

                return (errors);
        }

    public void reset() {
      log.info(" reset();");

      values.clear();

      // add some fields I want ...
      setValue("prevState" , new java.lang.String() );
      setValue("state" , new java.lang.String() );
      setValue("valid" , new java.lang.String() );
      setValue("validateModel" , new java.lang.String() );
    }

}

[validation.xml]

    11     <formset>
    33        <form name="/login">
    34          <field
                        <!-- a dynamic map backed property -->
    35            property="value(password)"
    36            depends="required" >
    37            <arg0 key="loginForm.password.error.label" />
    38          </field>
    40          <field
    41            property="value(email)"
    42            depends="required,email" >
    43            <arg0 key="loginForm.email.error.label" />
    44          </field>
    45        </form>
    57     </formset>
    59  </form-validation>



-----Original Message-----
From: Chalkidis Spyros [mailto:[EMAIL PROTECTED]
Sent: Monday, March 17, 2003 4:26 AM
To: [EMAIL PROTECTED]
Subject: Real dynamic properties



        Hello,

        is there a way to configure the properties of a DynaActionForm
really dynamically? I mean not to
define them in the struts-config.xml but through java code?

        Thanks in advance,

        Spyros Halkidis

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

Reply via email to