[Jakarta-commons Wiki] Update of "BeanUtils/FAQ" by SimonKitching

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[Jakarta-commons Wiki] Update of "BeanUtils/FAQ" by SimonKitching

Apache Wiki
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Jakarta-commons Wiki" for change notification.

The following page has been changed by SimonKitching:
http://wiki.apache.org/jakarta-commons/BeanUtils/FAQ

------------------------------------------------------------------------------
  If you want BeanUtils to do implicit String->Date conversions for you, then you just need to register a suitable converter for the date
  formats you expect to encounter in your input.
 
+ === How can I correctly convert locale-specific input? ===
+
+ If your code is directly calling conversion-related methods on ConvertUtils/ConvertUtilsBean to do data conversion, then you can simply
+ change to using LocaleBeanUtils/LocaleBeanUtilsBean/LocaleConvertUtils/LocaleConvertUtilsBean instead. The Locale-aware classes will
+ automatically detect the locale of the host machine and set up appropriate converters for that locale. Alternatively you can explicitly
+ create LocaleConvertUtilsBean instances providing a particular locale.
+
+ If your code is calling the property-related methods on BeanUtils/BeanUtilsBean methods, and you want the automatic type conversion
+ facilities used to be locale-aware then you might want to look at using the equivalent methods on the LocaleBeanUtils or LocaleBeanUtilsBean
+ classes. However because the property-related methods on these classes are not used nearly as often as the property methods on the standard
+ (non-locale-aware) classes, they may not be as well debugged and some features may be missing.
+
+ A safer alternative to either of the above is to register custom locale-aware converters with ConvertUtils or ConvertUtilsBean:
+ {{{
+   LongLocaleConverter  longLocaleConverter = new LongLocaleConverter(Locale.GERMAN);
+   ConvertUtils.register(longLocaleConverter, Long.class);
+   // now any call to any method on the BeanUtils or ConvertUtils classes which involves
+   // converting a string to a Long object will use a LongLocaleConverter which is customised
+   // to handle the German locale.
+ }}}
+
+ Of course the above will modify the default behaviour across the entire current application (or the current webapp if the code is
+ running in a container environment; see the javadoc for method BeanUtils.getInstance for more information).
+
+ If you do not like the idea of changing the ConvertUtils/BeanUtils behaviour so widely, then of course you can always
+ create a BeanUtilsBean instance and customise only the behaviour of that instance:
+ {{{
+   ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();
+   // here, customise the convertUtilsBean as required by registering custom converters
+
+   PropertyUtilsBean propertyUtilsBean = new propertyUtilsBean();
+   BeanUtilsBean beanUtilsBean = new BeanUtilsBean(convertUtilsBean, propertyUtilsBean);
+
+   // now methods on the beanUtilsBean object will use the custom converters registered
+   // on the associated ConvertUtilsBean instance.
+ }}}
+
+ === How can I customise the conversion? ===
+
+ If you don't like the default way beanutils converts strings to various datatypes, then simply register a custom converter.
+
+ So for example if you would like whitespace to be ignored when converting strings to numeric values, then create your own
+ converter classes and register them with ConvertUtils for the datatypes you want to be affected. Note that in this case
+ it would be easier to write a generic "filter" class that wraps the existing converters rather than create new converters
+ classes:
+
+ {{{
+   private static class WhiteSpaceConverterFilter implements Converter {
+     Converter delegate;
+
+     public WhiteSpaceConverterFilter(Converter delegate) {
+       this.delegate = delegate;
+     }
+
+     public Object convert(Class clazz, Object value) {
+       if (value instanceof String) {
+         return delegate.convert(clazz, value.toString().trim());
+       } else {
+         return delegate.convert(clazz, value);
+       }
+     }
+   }
+
+   ConvertUtils.register(new WhiteSpaceConverterFilter(new IntegerConverter()), Integer.TYPE);
+   ConvertUtils.register(new WhiteSpaceConverterFilter(new IntegerConverter(), Integer.class);
+   ConvertUtils.register(new WhiteSpaceConverterFilter(new LongConverter()), Long.TYPE);
+   ConvertUtils.register(new WhiteSpaceConverterFilter(new LongConverter()), Long.class);
+   ....
+ }}}
+
+ One particular case of customising conversion is to make the conversion locale-aware. See the
+ FAQ entry titled "How can I correctly convert locale-specific input?" for specific information
+ about this.
+

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]