Thank you Will, this is very cool. Much better than littering my form beans 
with desribe methods.

mark n.


>From: Will Jaynes <[EMAIL PROTECTED]>
>Reply-To: "Struts Users Mailing List" <[EMAIL PROTECTED]>
>To: [EMAIL PROTECTED]
>Subject: Re: Custom describe() for use with BeanUtil.populate()
>Date: Tue, 21 May 2002 07:12:46 -0400
>
>You don't need your own describe method anymore. BeanUtils has added the
>capability to register conversion methods, to go from String to any
>arbitrary object. So for example, I have a MSCalendar object whose
>toString puts out mm/dd/yyyy format, and can take that same format in a
>constructor.
>
>The MSConverter (attached) is mostly copied from one of Craig's
>converters in the Commons BeanUtils package.
>
>public void testStuff()
>     {
>         Map map = null;
>         int i;
>
>         // Register converters that enable conversion of String properties
>         // to a designated class. String->MSCalendar
>         ConvertUtils.register(new MSCalendarConverter(),
>                               MSCalendar.class);
>
>         EmployeeBean emp = new EmployeeBean();
>         emp.setFirstName("Joe");
>         emp.setLastName("Klein");
>         emp.setBirthDate(new MSCalendar("1/1/1999"));
>
>         EmployeeFormBean form = new EmployeeFormBean();
>         try {
>
>     // Copy all properties of an EmployeeBean to the corresponding
>     // properties of an EmployeeFormBean.
>     // This process uses the toString() method of each
>     // property in EmployeeBean as the value to put into
>     // the corresponding String property in the form.
>             map = BeanUtils.describe(emp);
>             BeanUtils.populate(form, map);
>
>     // Conversely, copy all the properties from the form into
>     // the corresponding properties of EmployeeBean, converting
>     // from String to the taget object type.
>             emp = new EmployeeBean();
>             map = BeanUtils.describe(form);
>             BeanUtils.populate(emp, map);
>
>          } catch(Exception ex) {
>             fail("failure:"+ex.getStackTrace());
>         }
>     }
>}
>
>DHS Struts wrote:
>>I was wondering if anyone had a custom describe method they would be
>>willing to share as an example of what Ted Husted describes in his
>>"Struts Catalog" in the 'Use a "populate" utility to exchange data with
>>value objects' section?
>>
>>I understand the basic concept of needing to write my own describe() for
>>those beans that have Date attributes (because the BeanUtil.populate()
>>can't handle them), but I am not having much luck getting it to work.
>>
>>If you have some code and are willing to share I would appreciate it.
>>Also, if you would like to see what I have attempted I can post that
>>here as well.
>>
>>Thank you in advance,
>>
>>mark n.
>>
>>_________________________________________________________________
>>Chat with friends online, try MSN Messenger: http://messenger.msn.com
>>
>>
>>--
>>To unsubscribe, e-mail:
>><mailto:[EMAIL PROTECTED]>
>>For additional commands, e-mail:
>><mailto:[EMAIL PROTECTED]>
>>
>
>
>package edu.umich.umms.beanutils;
>import java.sql.Date;
>import org.apache.commons.beanutils.ConversionException;
>import org.apache.commons.beanutils.Converter;
>
>
>/**
>  * <p>Standard {@link Converter} implementation that converts an incoming
>  * String into a <code>edu.umich.umms.util.MSCalendar</code> object, 
>optionally using a
>  * default value or throwing a {@link ConversionException} if a conversion
>  * error occurs.</p>
>  *
>  */
>
>public final class MSCalendarConverter implements Converter {
>
>
>     // ----------------------------------------------------------- 
>Constructors
>
>
>     /**
>      * Create a {@link Converter} that will throw a {@link 
>ConversionException}
>      * if a conversion error occurs.
>      */
>     public MSCalendarConverter() {
>
>         this(null);
>
>     }
>
>
>     /**
>      * Create a {@link Converter} that will return the specified default 
>value
>      * if a conversion error occurs.
>      *
>      * @param defaultValue The default value to be returned
>      */
>     public MSCalendarConverter(Object defaultValue) {
>
>         this.defaultValue = defaultValue;
>
>     }
>
>
>     // ----------------------------------------------------- Instance 
>Variables
>
>
>     /**
>      * The default value specified to our Constructor, if any.
>      */
>     private Object defaultValue = null;
>
>
>     // --------------------------------------------------------- Public 
>Methods
>
>
>     /**
>      * Convert the specified input object into an output object of the
>      * specified type.
>      *
>      * @param type Data type to which this value should be converted
>      * @param value The input value to be converted
>      *
>      * @exception ConversionException if conversion cannot be performed
>      *  successfully
>      */
>     public Object convert(Class type, Object value) {
>
>         if (value == null) {
>             if (defaultValue != null) {
>                 return (defaultValue);
>             } else {
>                 throw new ConversionException("No value specified");
>             }
>         }
>
>         try {
>             return new edu.umich.umms.util.MSCalendar((String) value);
>//            return (Date.valueOf((String) value));
>         } catch (Exception e) {
>             if (defaultValue != null) {
>                 return (defaultValue);
>             } else {
>                 throw new ConversionException(e);
>             }
>         }
>
>     }
>
>
>}
>
>--
>To unsubscribe, e-mail:   
><mailto:[EMAIL PROTECTED]>
>For additional commands, e-mail: 
><mailto:[EMAIL PROTECTED]>


_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com


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

Reply via email to