2011年11月7日 星期一

ConversionException: No value specified for 'Date'

轉載自 http://www.mail-archive.com/user@commons.apache.org/msg02246.html





原因:當JavaBean用到了Date等非內建型態時,如果Object為NULL則會出現此異常。

解決方法:
You have a choice of two flavours for copying properties in BeanUtils:


1) Usng PropertyUtils copyProperties() method[1] - this just copies

any properties unchanged

2) Using BeanUtils copyProperties() method[2] - copies AND converts

properties - so for example, if theres a property called "foo" of type

String in the source and a property called "foo" of type Integer in

the destination then it tries to convert the String to an Integer

using the Converter registered for Integers.



If you don't need conversion then you can switch to using

PropertyUtils. If however you have some conversion needs, but you

don't want it to throw an exception when a value is "null" (which is

the default behaviour) - then you can configure a converter for Date

which has a default value. So for example, if you wanted a default of

"null" for Dates, then something like the following:



java.util.Date defaultValue = null;

DateConverter converter = new DateConverter(defaultValue);

ConvertUtils.register(converter, java.util.Date.class);



Then when you call BeanUtils.copyProperties() it should no longer

throw an exception.



[1] http://tinyurl.com/4rc5ho

[2] http://tinyurl.com/44csva

沒有留言:

張貼留言