commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Niall Pemberton <niall.pember...@gmail.com>
Subject Re: [beanutils] Problems with BeanUtilsBean : Convert Date to String
Date Wed, 18 May 2011 20:10:31 GMT
On Sat, Apr 23, 2011 at 9:32 AM, d@v1d v00 <davidvoo@gmail.com> wrote:
> Hi there,
>
> Has anyone done this before to populate HTML form data into JavaBeans that
> has Date data type? I really stucked here and hope this mailing list (apache
> commons) can help me.
>
> If I can't get help from the source, i have to dtich apache commons
> beansutil project to be used anymore.

You can configure the date converter with a set of patterns for the
String formats you're expecting and register that, something like:

String[] patterns = new String[] {"yyyy-MM-dd", "dd-MM-yyyy"};
DateTimeConverter converter = new DateTimeConverter();
converter.setPatterns(patterns);
ConvertUtils.register(converter, Date.class);

See http://tinyurl.com/6xz9fe8 for more options

Niall

> Thanks,
>
> David
>
>
> On Sat, Apr 23, 2011 at 8:37 AM, d@v1d v00 <davidvoo@gmail.com> wrote:
>
>> There is no such argument for the switch.
>>
>> On Saturday, April 23, 2011, Martin Gainty <mgainty@hotmail.com> wrote:
>> >
>> > org.apache.commons.beanutils.ConvertUtilsBean
>> > {
>> > private void register(Class clazz, org.apache.commons.beanutils.Converter
>> converter)
>> > }
>> >
>> > switch the arguments
>> >
>> >
>> > Martin Gainty
>> > ______________________________________________
>> > Jogi és Bizalmassági kinyilatkoztatás/Verzicht und
>> Vertraulichkeitanmerkung/Note de déni et de confidentialité
>> >  Ez az
>> > üzenet bizalmas.  Ha nem ön az akinek szánva volt, akkor kérjük, hogy
>> > jelentse azt nekünk vissza. Semmiféle továbbítása vagy másolatának
>> > készítése nem megengedett.  Ez az üzenet csak ismeret cserét szolgál
és
>> > semmiféle jogi alkalmazhatósága sincs.  Mivel az electronikus üzenetek
>> > könnyen megváltoztathatóak, ezért minket semmi felelöség nem terhelhet
>> > ezen üzenet tartalma miatt.
>> >
>> > Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene
>> Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte
>> Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht
>> dient lediglich dem Austausch von Informationen und entfaltet keine
>> rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von
>> E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
>> > Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas
>> le destinataire prévu, nous te demandons avec bonté que pour satisfaire
>> informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie
>> de ceci est interdite. Ce message sert à l'information seulement et n'aura
>> pas n'importe quel effet légalement obligatoire. Étant donné que les email
>> peuvent facilement être sujets à la manipulation, nous ne pouvons accepter
>> aucune responsabilité pour le contenu fourni.
>> >
>> >
>> >
>> >
>> >> Date: Sat, 23 Apr 2011 01:20:03 +0800
>> >> Subject: [beanutils] Problems with BeanUtilsBean : Convert Date to
>> String
>> >> From: davidvoo@gmail.com
>> >> To: user@commons.apache.org
>> >>
>> >> I am trying to run BeanUtilsBean.getInstance().populate(...) but on the
>> HTML
>> >> form, there is a field that carries String representation of Date of
>> Birth.
>> >> The object bean has the field type of java.util.Date
>> >>
>> >> Read some search from Ggl that have to build custom converters but not
>> quite
>> >> understand how to do that.
>> >>
>> >> Anyone can help?
>> >>
>> >> My code:
>> >>
>> >> public static void main(String[] args) {
>> >>
>> >>     Map<String, String[]> formData = new HashMap<String, String[]>();
>> >>
>> >>     formData.put("email", new String[]{"danny@gmail.com"});
>> >>
>> >>     formData.put("firstName", new String[]{"danny"});
>> >>
>> >>     formData.put("lastName", new String[]{"miller"});
>> >>
>> >>     formData.put("dob", new String[]{"15-Apr-1980"});
>> >>
>> >>     formData.put("userName", new String[]{"dannymiller"});
>> >>
>> >>     try {
>> >>         Consumer consumer = new Consumer();
>> >>
>> >>         DateTimeConverter dtConverter = new DateConverter();
>> >>
>> >>         dtConverter.setPattern("dd/MMM/yyyy");
>> >>
>> >>         ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();
>> >>
>> >>         convertUtilsBean.deregister(Date.class);
>> >>
>> >>         convertUtilsBean.register(dtConverter, Date.class);
>> >>
>> >>         BeanUtilsBean beanUtilsBean = new
>> >> BeanUtilsBean(convertUtilsBean, new PropertyUtilsBean());
>> >>
>> >>         beanUtilsBean.populate(consumer, formData);
>> >>
>> >>
>> >>         if (consumer != null) {
>> >>
>> >>             System.out.println(consumer.getEmail());
>> >>
>> >>             System.out.println(consumer.getFirstName());
>> >>
>> >>             System.out.println(consumer.getLastName());
>> >>
>> >>             System.out.println(consumer.getDob());
>> >>
>> >>             System.out.println(consumer.getUserName());
>> >>
>> >>         }
>> >>     } catch  (Exception e) {
>> >>
>> >>         e.printStackTrace();
>> >>     }
>> >> }
>> >>
>> >> The return error:
>> >>
>> >> Apr 22, 2011 11:14:45 PM
>> >> org.apache.commons.beanutils.converters.DateTimeConverter toDate
>> WARNING:
>> >> DateConverter does not support default String to 'Date' conversion. Apr
>> 22,
>> >> 2011 11:14:45 PM
>> org.apache.commons.beanutils.converters.DateTimeConverter
>> >> toDate WARNING: (N.B. Re-configure Converter or use alternative
>> >> implementation) Exception in thread "main"
>> >> org.apache.commons.beanutils.ConversionException: DateConverter does not
>> >> support default String to 'Date' conversion. at
>> >>
>> org.apache.commons.beanutils.converters.DateTimeConverter.toDate(DateTimeConverter.java:468)
>> >> at
>> >>
>> org.apache.commons.beanutils.converters.DateTimeConverter.convertToType(DateTimeConverter.java:343)
>> >> at
>> >>
>> org.apache.commons.beanutils.converters.AbstractConverter.convert(AbstractConverter.java:156)
>> >> at
>> >>
>> org.apache.commons.beanutils.converters.ConverterFacade.convert(ConverterFacade.java:60)
>> >> at
>> >>
>> org.apache.commons.beanutils.ConvertUtilsBean.convert(ConvertUtilsBean.java:470)
>> >> at
>> >>
>> org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1008)
>> >> at
>> >>
>> org.apache.commons.beanutils.BeanUtilsBean.populate(BeanUtilsBean.java:830)
>> >> at com.ymatch.test.BeanTest.main(BeanTest.java:32)
>> >
>>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message