I've tinkered with this, but there are definite limitations.  I saw in Chuck
Cavaness' book where he put a value or domain object as the sole property
inside a form.  In the JSPs he references it as form.bean.blahblahblah.  I
thought this meant I wouldn't have to dumb-down my forms.  Well, as you've
seen, BeanUtils.copyProperties() won't copy when it doesn't recognize the
target class.  You can create a Converter to handle some of these
situations.  I created an AbstractConverter class that has abstract
getToClass() and getFromClass() methods.  My convert() implementation just
uses getToClass() to create an approriate target object (using
Class.newInstance()), then calls BeanUtils.copyProperties on it.  For simple
situations, this works really well.  However, there are some limitations.
First, you can't have circular references because there's no mechanism in
place to note whether the copying of a given object is already in progress.
You'll get a StackOverflowError.  I think I know how to get around this but
it's pretty invasive.  Second, you can't copy properties that are of the
same type as the containing object easily.  You'd have to duplicate a lot
(all?) of the functionality of copyProperties().  Third, the contents of
collections are a problem because you won't know what type the contents of
the new collectin should be, unless you add some other facility for figuring
this out (like "given value object type X, convert to domain object type Y."
This is the opposite of how the Converters work now.)  Finally, if you're
copying from a persistent object that uses some form of pointer swizzling to
a value object, you've opened a whole new can of worms.

I don't know what my fallabck solution is exactly.  I think I might pick a
few limitations and live with them for now.

john

-----Original Message-----
From: Molitor, Stephen [mailto:[EMAIL PROTECTED]] 
Sent: Tuesday, January 28, 2003 8:30 AM
To: 'Struts Users Mailing List'
Cc: Ramarao, Nagaraj; Nair, Pramod
Subject: Copying 'nested' properties from Dyna form to beans


What's the best way to copy 'nested' form property values to Java beans? For
example, let's say I a 'Person' bean, which contains a nested 'Name' bean,
like this:

class Name {
  String getFirstName() {...}
  void setFirstName(String firstName) {...}

  String getLastName() {...}
  void setLastName(String lastName) {...}
}

class Person {
  Name getName() {...}
  void setName(Name name) {...}

  String getZip() {...}
  void setZip(String zip) {...}
}

In the Person class, 'zip' is a top level property, but firstName and
lastName are nested properties of the name property.  I'd like to set up my
validation.xml form element something like this:

<form name="personForm">
  <field property="name.firstName"/>
  <field property="name.lastName"/>
  <field property="zip"/>
</form>

Or, can I / should I nest form elements?

Finally, what I'd like to do is use BeanUtils.copyProperties, or whatever,
to copy from the dyna bean to the Person bean:

  BeanUtils.copyProperties(person, form);

It would be cool if copyProperties would know to copy the name.firstName
form element to person.getName().setFirstName(...).   

What's the best way to do something like this, to copy 'nested' properties
transparently in one method call?  

Thanks!

Steve
  


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

Reply via email to