You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by se...@bkw-fmb.ch on 2002/10/18 12:21:25 UTC

AW: BeanUtils.copyProperties(formBean, javaBean) not converting S trin g dates to java.util.Date dates

Hi,

have you tried the other way round?

BeanUtils.copyProperties(javaBean, formBean)

robert


> -----Ursprüngliche Nachricht-----
> Von: Clayson, Jim [mailto:Jim.Clayson@centrica.co.uk]
> Gesendet: Donnerstag, 17. Oktober 2002 18:38
> An: 'struts-user@jakarta.apache.org'
> Betreff: BeanUtils.copyProperties(formBean, javaBean) not converting
> Strin g dates to java.util.Date dates
> 
> 
> Hi,
> 
> Struts1.1b2: BeanUtils.copyProperties(formBean, javaBean) not 
> converting
> String properties into Dates:
> 
> I am trying to figure out what is wrong with what I am doing. 
> I have a form
> bean with date string properties (from and to) which I would 
> like to copy
> into a javabean class. When I print out some debug 
> statements, I can see the
> string values i.e. 04/09/2002 and 19/09/2002, but the same 
> properties (which
> are named the same and have identicale getters and setters) 
> in the javaBean
> class show null after I do:
> 
> 	BeanUtils.copyProperties(formBean, javaBean);
> 
> 
> Can anyone help me out?
> 
> Jim
> 
> 
> ------------------------------------------------------------
> The information contained in or attached to this email is
> intended only for the use of the individual or entity to
> which it is addressed. If you are not the intended
> recipient, or a person responsible for delivering it to the
> intended recipient, you are not authorised to and must not
> disclose, copy, distribute, or retain this message or any
> part of it. It may contain information which is confidential
> and/or covered by legal professional or other privilege (or
> other rules or laws with similar effect in jurisdictions
> outside England and Wales).
> 
> The views expressed in this email are not necessarily the
> views of Centrica plc, and the company, its directors,
> officers or employees make no representation or accept any
> liability for its accuracy or completeness unless expressly
> stated to the contrary.
> 
> 
> --
> To unsubscribe, e-mail:   
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: AW: BeanUtils.copyProperties(formBean, javaBean) not converting S trin g dates to java.util.Date dates

Posted by Rick Reumann <ma...@reumann.net>.
>> 
>> Hi,
>> 
>> Struts1.1b2: BeanUtils.copyProperties(formBean, javaBean) not 
>> converting
>> String properties into Dates:
>> 

   It will not covert them into java.util.Date. You need to create a
   Converter class to do it an register it. I just went through all
   this with Eldercle who posted to this list as well. The solution
   that works is below. Just create the two classes below and register
   them in the action like I do below and you should be all set. Let
   me know if you have any questions.

To use these converters so that BeanUtils.copyProperties( ) works in
both directions I just added a static block to the top of my dispatch
Action ...

static {
  DateBeanUtilsConverter dateConverter = new DateBeanUtilsConverter();
  dateConverter.setFormatPattern( "MMddyyyy" );
  StringBeanUtilsConverterDate myStringConverter = new StringBeanUtilsConverterDate();
  myStringConverter.setFormatPattern( "MMddyyyy" );
  ConvertUtils.register( dateConverter, java.util.Date.class );
  ConvertUtils.register( myStringConverter, String.class );
}

The two classes are listed below. All seems to be working fine.

/** coverts java.util.Date to String using BeanUtils ***/

import org.apache.commons.beanutils.Converter;
import java.text.*;
import java.util.*;
import corporate.*;

public class DateBeanUtilsConverter implements Converter {

    private String formatPattern = null;

    public void setFormatPattern(String formatPattern) {
        this.formatPattern = formatPattern;
    }

    public Object convert(Class type, Object value) {
        Date date = null;

        if (value != null
            && (value instanceof String)
            && (type == Date.class)) {
            try {

                String s = value.toString();
                SimpleDateFormat formatter =
                    new SimpleDateFormat(formatPattern);
                date = formatter.parse(s);

            } catch (Exception e) {
                ErrorLogging.println("DateBeanUtilsConverter: " + e);
            }
        }
        return date;
    }
}

/** coverts String to java.util.Date using BeanUtils ***/

import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.converters.*;
import java.text.*;
import java.util.*;
import corporate.*;

public class StringBeanUtilsConverterDate implements Converter {
    private static final StringConverter stringConverter =
        new StringConverter();
    private String formatPattern = null;

    public void setFormatPattern(String formatPattern) {
        this.formatPattern = formatPattern;
    }

    public Object convert(Class type, Object value) {
        Object returnValue = null;

        if (value != null) {
            if (type == String.class && (value instanceof Date)) {
                SimpleDateFormat formatter =
                    new SimpleDateFormat(formatPattern);
                String dateString = formatter.format(value);
                returnValue = dateString;
            } else {
                returnValue = stringConverter.convert(type, value);
            }
        }
        return returnValue;
    }
}
   

--

Rick

mailto:maillist@reumann.net


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>